Objective-C中NSArray的基本用法示例
NSArray的排序
+(id)studentWithFirstName:(NSString*)firstNamelastName:(NSString*)lastName{ Student*stu=[[Studentalloc]init]; stu.firstName=firstName; stu.lastName=lastName; returnstu; } +(id)studentWithFirstName:(NSString*)firstNamelastName:(NSString*)lastNamebookName:(NSString*)bookName{ Student*stu=[StudentstudentWithFirstName:firstNamelastName:lastName]; stu.book=[BookbookWithName:bookName]; returnstu; } -(NSComparisonResult)compareStudent:(Student*)stu{ NSComparisonResultresult=[self.firstNamecompare:stu.firstName]; if(result==NSOrderedSame){ result=[self.lastNamecompare:stu.lastName]; } returnresult; } -(NSString*)description{ //return[NSStringstringWithFormat:@"%@%@%@",self.firstName,self.lastName,self.book.name]; return[NSStringstringWithFormat:@"%@%@%@",self.firstName,self.lastName,_book.name]; } #pragmamark3.NSArray排序1 voidarraySort1(){ NSArray*array=[NSArrayarrayWithObjects:@"2",@"3",@"1",@"4",nilnil]; //指定系统自带规定的比较方法compare: NSArray*array2=[arraysortedArrayUsingSelector:@selector(compare:)]; NSLog(@"%@",array2); } #pragmamark3.NSArray排序2 voidarraySort2(){ Student*stu1=[StudentstudentWithFirstName:@"hu"lastName:@"mingtao"]; Student*stu2=[StudentstudentWithFirstName:@"zhu"lastName:@"wenpeng"]; Student*stu3=[StudentstudentWithFirstName:@"zhao"lastName:@"weisong"]; Student*stu4=[StudentstudentWithFirstName:@"hu"lastName:@"junpeng"]; NSArray*array=[NSArrayarrayWithObjects:stu1,stu2,stu3,stu4,nilnil]; //类似JAVA中得compareTo,自己定义比较方式,但是一定要实现compare方法 NSArray*array2=[arraysortedArrayUsingSelector:@selector(compareStudent:)]; NSLog(@"%@",array2); } #pragmamark3.NSArray排序3-Block排序 voidarraySort3(){ Student*stu1=[StudentstudentWithFirstName:@"hu"lastName:@"mingtao"]; Student*stu2=[StudentstudentWithFirstName:@"zhu"lastName:@"wenpeng"]; Student*stu3=[StudentstudentWithFirstName:@"zhao"lastName:@"weisong"]; Student*stu4=[StudentstudentWithFirstName:@"hu"lastName:@"junpeng"]; NSArray*array=[NSArrayarrayWithObjects:stu1,stu2,stu3,stu4,nilnil]; NSArray*array2=[arraysortedArrayUsingComparator:^NSComparisonResult(Student*obj1,Student*obj2){ NSComparisonResultresult=[obj1.firstNamecompare:obj2.firstName]; if(result==NSOrderedSame){ result=[obj1.lastNamecompare:obj2.lastName]; } returnresult; }]; NSLog(@"%@",array2); } #pragmamark4.NSArray排序4-高级排序 voidarraySort4(){ Student*stu1=[StudentstudentWithFirstName:@"hu"lastName:@"mingtao"bookName:@"lianai"]; Student*stu2=[StudentstudentWithFirstName:@"zhu"lastName:@"wenpeng"bookName:@"tianshi"]; Student*stu3=[StudentstudentWithFirstName:@"zhao"lastName:@"weisong"bookName:@"love"]; Student*stu4=[StudentstudentWithFirstName:@"hu"lastName:@"junpeng"bookName:@"qingren"]; NSArray*array=[NSArrayarrayWithObjects:stu1,stu2,stu3,stu4,nilnil]; //1.先按照书名进行排序 NSSortDescriptor*bookNameDesc=[NSSortDescriptorsortDescriptorWithKey:@"book.name"ascending:YES]; //2.先按照姓进行排序 NSSortDescriptor*firstNameDesc=[NSSortDescriptorsortDescriptorWithKey:@"firstName"ascending:YES]; //3.先按照名进行排序 NSSortDescriptor*lastNameDesc=[NSSortDescriptorsortDescriptorWithKey:@"lastName"ascending:YES]; NSArray*array2=[arraysortedArrayUsingDescriptors:[NSArrayarrayWithObjects:bookNameDesc,firstNameDesc,lastNameDesc,nilnil]]; NSLog(@"%@",array2); }
NSArray的一些用法
NSArray 只允许装OC对象,并且不能装空值,空代表数组元素的结束
#pragmamark-NSArray的基本用法 //创建一个空数组 NSArray*array=[NSArrayarray]; //创建有一个元素的数组 array=[NSArrayarrayWithObject:@"123"]; //创建有多个元素的数组 array=[NSArrayarrayWIthObjects:@"a",@"b",nil];//不能装nil空指针,空值代表数组元素结束 //将一个数组赋值给一个数组 +(instancetype)arrayWithArray:(NSArray*)array; //获取元素的个数 intcount=[arraycount];//和count=array.count;相同,都是调用get方法 //是否包含一个元素 -(bool)containsObject:(id)anObject; //返回最后一个元素 -(id)lastObject; //获取index位置的元素 -(id)objectAtIndex:(NSUInteger)index; //获取元素的位置 -(NSUInteger)indexOfObject:(id)anObject; //在range范围内查找元素的位置 -(NSUInteger)indexofObject:(id)anObjectinRange:(NSRange)range; //比较两个集合内容是否相同 -(Bool)isEqualToArray:(NSArray*)otherArray; //返回两个集合中第一个相同的对象元素 -(id)firstObjectCommonWithArray:(NSArray*)otherArray;
#pragmamark-NSArray的高级用法 //让集合里面的所有元素都执行aSelector这个方法 -(void)makeObjectsPerformSelector:(SEL)aSelector; //让集合里面的所有元素都执行aSelector这个方法,给这个方法添加参数,但是只支持一个参数 -(void)makeObjectsPerformSelector:(SEL)aSelectorwithObject:(id)argument //添加一个元素,返回一个新的NSArray(方法调用者本身没有发生变化) -(NSArray*)arrayByAddingObject:(id)anObject //添加otherArray的所有元素,返回一个新的NSArray(方法着本身没有改变) -(NSArray*)arrayByAddingObjectsFromArray:(NSArray*)otherArray; //截取range范围的数组 -(NSArray*)subarrayWithRange:(NSRenge)range; //用separator做拼接符,拼接成一个字符串 -(NSString*)componentsJoinedByString:(NSString*)separator //将NSArray持久化到文件中去 -(Bool)writeToFile:(NSString*)pathatomically:(Bool)useAuxiliaryFile
#pragmamark-NSArray的遍历 //方法一:普通遍历(利用for循环) voidarrayFor1(){ NSArray*array=[NSArrayarrayWithObjects:@"1",@"2",@"3",nil]; intcount=array.count; for(inti=0;i<count;i++){ idobj=[arrayobjectAtIndex:i]; NSLog(@"%i-%@",i,obj); } }
//方法二:快速遍历 voidarrayFor2(){ NSArray*array=[NSArrayarrayWithObjects:@"1",@"2",@"3",nil]; intcount=array.count; inti=0; for(idobjinarray){ NSLog(@"%i-%@",i,obj); i++; } }
//方法三:利用block遍历 voidarrayFor3(){ NSArray*array=[NSArrayarrayWithObjects:@"1",@"2",@"3",nil]; [arrayenumerateObjectsUsingBlock:^(idobj,NSUIntegeridx,BOOL*stop){ NSLog(@"%zi->%@",idx,obj); // *stop=YES;//改变外边的Bool,终止遍历 }]; }
//方法四:利用迭代器 先介绍一下-->NSEnumerator迭代器:集合的迭代器,可以用于遍历集合元素,NSArray有相应的方法来获取迭代器 //获取一个正序遍历的迭代器 -(NSEnumerator*)objectEnumerator; //获取一个反序遍历的迭代器 -(NSEnumerator*)reverseObjectEnumerator; @常用方法: //获取下一个元素 -(id)nextObject; //获取所有的元素 -(NSArray*)allObjects voidarrayFor4(){ NSArray*array=[NSArrayarrayWithObjects:@"1",@"2",@"3",nil]; NSEnumerator*enumerator=[arrayobjectEnumerator];//返回数组的迭代器 //如果放到遍历之后,则取到空,原因是,遍历完了,就没值了 NSArray*array2=[enumeratorallObjects]; NSLog(@"array2=%@",array2); //获取下一个需要遍历的元素 idobj=nil; while(obj=[enumeratornextObject]){ NSLog(@"obj=%@",obj); } }