星空网 > 软件开发 > 操作系统

[oc学习笔记]不可变数组

     
NSArray *aa = [[NSArray alloc]init]; NSArray *aaa = [NSArray array]; //声明单个内容数组 NSArray *aaaa = [NSArray arrayWithObject:@"sd"]; //声明多个内容数组 NSArray *arr = [NSArray arrayWithObjects:@"SDS",@"Dsad", nil]; //数组的长度 NSLog(@"%zi",arr.count); //数组中某个下标对应的对象 NSLog(@"%@",[arr objectAtIndex:1]); //数组中最后一个对应的对象 NSLog(@"%@",[arr lastObject]); //数组中某个元素对应的下标 NSLog(@"%zi",[arr indexOfObject:@"Dsad"]); //写入文件 NSString *path = @"/Users/XuLee/Desktop/oc/abc.txt"; [arr writeToFile:path atomically:YES]; //读文件必须是特定格式 NSString *ff = [NSArray arrayWithContentsOfFile:path]; //把数组的对象中间加一个东西连接成一个字符串 NSString *fg = [arr componentsJoinedByString:@"-"]; //数组的遍历 //普通遍历方式 for (int i = 0; i < arr.count; i++) { id obj = [arr objectAtIndex:i]; NSLog(@"%@",obj); } //快速遍历 for (id obj in arr ) { NSLog(@"%@",obj); } //代码块遍历 [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) // obj为元素 idx 为下标 stop相当于break stop为yes时 循环会结束 { NSLog(@"%@",obj); //stop 赋值形式为 *stop = yes or no }]; //迭代器遍历 //将数组中各个元素装入迭代器中 NSEnumerator * vv = [arr objectEnumerator]; id obj = nil; while (obj = [vv nextObject]) { NSLog(@"%@",obj); } //反向迭代 NSEnumerator *vvv = [arr reverseObjectEnumerator]; while (obj = [vvv nextObject]) { NSLog(@"%@",obj); } //让数组统一调用某方法 Student *stu1 = [[Student alloc]init]; stu1.name = @"fff"; Student *stu2 = [[Student alloc]init]; stu2.name = @"hhh"; NSArray *aee = [NSArray arrayWithObjects:stu1,stu2,nil]; [aee makeObjectsPerformSelector:@selector(sayHi)]; //让数组统一调用某一带参数的方法 [aee makeObjectsPerformSelector:@selector(study:) withObject:@"math"];

-(NSString *)description{  NSString *aa = [NSString stringWithFormat:@"%@",_name];  return aa;}-(void)sayHi{  NSLog(@"my name %@",_name);}-(void)study:(NSString *)ss{  NSLog(@"%@ study %@",_name,ss);}

@property (nonatomic,strong)NSString *name;-(void)sayHi;-(void)study:(NSString *)ss;

 




原标题:[oc学习笔记]不可变数组

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

价之链_价之链电商:https://www.ikjzd.com/w/1033
环球易购:https://www.ikjzd.com/w/1034
Gearbest平台介绍_:https://www.ikjzd.com/w/1035
帕拓逊patozon介绍:https://www.ikjzd.com/w/1036
百事泰介绍:https://www.ikjzd.com/w/1037
环球易购服装平台Zaful介绍:https://www.ikjzd.com/w/1038
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流