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;
时间: 2024-12-27 22:37:09