你的位置:首页 > 操作系统

[操作系统][oc学习笔记]数组


    NSArray *arr = @[@"dsd",@"43"];    // 在原有数组后面加一个元素,原数组不变,产生新数组    NSArray *arr1 = [arr arrayByAddingObject:@"saddas"];    //在原数组后面加一个数组,产生新数组,原数组不变    NSArray *atrr2 = [arr arrayByAddingObjectsFromArray:arr];    //取出原数组制定范围元素    NSArray *aa = [arr subarrayWithRange:NSMakeRange(2,2)];            //选择器排序    //一定要写selector 常用比较方法为compare    NSArray *arr3 = [arr sortedArrayUsingSelector:@selector(compare:)];    //代码块排序    NSArray *arr4 = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {      return [obj1 compare:obj2];    }];    //比较描述器排序    //常用于多个条件的比较    //存放比较描述器    NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"ss" ascending:YES];    NSArray *d = @[sort1];    NSArray *arr5 = [arr sortedArrayUsingDescriptors:d];        //可变数组定义    NSMutableArray *mu1 = [NSMutableArray array];    NSMutableArray *mu2 = [NSMutableArray arrayWithCapacity:10];    //可变数组尾部加元素    [mu1 addObject:@"sds"];    //可变数组尾部家数组    [mu1 addObjectsFromArray:@[@"d",@"ffgf"]];    //在什么位置插入    [mu1 insertObject:@"dd" atIndex:1];    //移除    [mu1 removeAllObjects];//全部    [mu2 removeLastObject];//最后    [mu2 removeObject:@"ff"];//特定元素    [mu2 removeObjectAtIndex:2];//特定位置    [mu2 removeObject:@"d" inRange:NSMakeRange(2, 1)];//特定范围的特定元素    //用指定字符替换制定位置的元素    [mu2 replaceObjectAtIndex:2 withObject:@"ff"];    //交换两个位置的元素    [mu2 exchangeObjectAtIndex:1 withObjectAtIndex:2];    //注意可变字符串与不可变字符串的排序方法的写法不同,此处不一一列举

 

     
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;