欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

不可变数组的一些方法与理解

程序员文章站 2022-07-07 11:22:38
...
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    /*______________________________不可变数组______________________________*/
   //创建
 NSString *s1 [email protected]"wangwei";
 NSString *s2 [email protected]"wangwang";
 NSString *s3 [email protected]"weiwei";
 NSArray *array1 = [[NSArray alloc] initWithObjects:s1,s2,s3, nil];
 NSLog(@"%@",array1.description);
    //类方法创建
 NSArray *array2=  [NSArray arrayWithObjects:s1,s2,s3,nil];
 NSArray *array3 =  [NSArray arrayWithObject:s1];//只能存一个.
    
 NSArray *array4=[NSArray arrayWithArray:array1];//从array1里面的元素全部存储到一个新的数组类
    
 NSLog(@"%@",array4);//打印array4.大致可以理解为备份一份数组里元素到一个新的数组类

   /*_____________________________通过下标取元素_________________________*/
 NSString *str1 =[array4 objectAtIndex:0];
 NSLog(@"%@",str1);
   /*_____________________________获取元素个____________________________________*/
    
     NSUInteger count1 =[array4 count];
     NSUInteger count2 = array4.count;//等价于[array4 count];
     NSLog(@"count2 = %ld",count2);
    /*_____________________________判断数组中是否包含某个对象__________________*/

    BOOL isContains =[array4 containsObject:@"wangwei"];
    NSLog(@"isContains:%d",isContains);
     /*___________________________查找某一个对象在数组中的下标位置_______________*/
    NSUInteger index =[array4 indexOfObject:@"wangwei"];   //表示@"wangwei"是数组里的第几个元   
                                                                                         //素,从0开始的,将下标给于 index
    NSLog(@"index =%ld",index);
    NSUInteger index1 =[array4 indexOfObject:@"wangweitaishuaile"];
    if(index1 ==NSNotFound){         //NSNotfound  表示没有这个元素          
                                                   // 枚举enum {NSNotFound =NSIntegerMax}"枚举括号里没看懂
    NSLog(@"没有这个元素");        
    }
    else {
        NSLog(@"index1=%ld",index1);
    }
   /*______________________连接数组中的字符串________________________________*/
    NSString *joinstring =[array4 componentsJoinedByString:@"________"];//返回一个连接之后的字符串
    NSLog(@"joinstring =%@",joinstring);   //必须数字里都是字符串才可以使用这个方法
    /*______________________访问最后一个字符串_______________________________*/
    NSString *lastobj1 =[array4 lastObject];//可以使用点语法  两个条件,方法名要一样,有返回值,其实就等于get方法,就可以用点语法
    NSString *lastobj2 =array4.lastObject;
    NSLog(@"lastobj:%@,%@",lastobj1,lastobj2);
    /*_______________________在原来数组后面追加一个元素________________________*/
    NSArray *array5 =[array4 arrayByAddingObject:@"wangwangwei"];
    //由于不可变,所以追加的是新生成的数组,原来数组中的元素无法更改
    NSLog(@"%@",array5);
   //NSArray *array6 =[NSArray arrayWithObjects: ]不可以存放基本数据类型放入数组,数组是存的是指针
    //如果传入的数组的下标超过数组最大元素,会错误的.数组越界index4  beyond bounds[0..3];
    //可以加入判断    if (idx<array.count) ;
    return ;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 下面是自己创的视图,以及理解,没有用到太多东西,按钮还是拖拽的

#import "ViewController.h"

@interface ViewController ()
{
    UIView *greenView;
}
@property (weak, nonatomic) IBOutlet UIView *orangeview;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    greenView =[[UIView alloc] initWithFrame:CGRectMake(100, 0,100, 200)];//X轴,Y轴,长,高
    greenView.backgroundColor =[UIColor greenColor];
    [_orangeview addSubview:greenView];     //这里是@property定义的属性用_orange,后面是对象,所以不需要
   //在orange视图上添加一个绿色视图
    UIView * yelloView=[[UIView alloc]initWithFrame:CGRectMake(100,0, 100,100)];
    yelloView.backgroundColor=[UIColor yellowColor];
    [greenView addSubview:yelloView];
    //在绿色视图上添加一个黄色师徒
}
- (IBAction)didtouch:(UITapGestureRecognizer*)sender {
    NSLog(@"汪伟,%ld",sender.state);
}
- (IBAction)longtouch:(UITapGestureRecognizer *)sender  //由于是从view控制那拖过来,类型是ID,因此state无法识别,需要告诉state
{                                                       //是UITapGestureRecognizer类型的指针
    NSLog(@"汪伟,%ld",sender.state);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end