生活随笔
收集整理的這篇文章主要介紹了
ios数组基本用法和排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.創建數組
[objc] view plaincopy
NSArray?*array?=?[NSArray?array];??????array?=?[NSArray?arrayWithObject:@"123"];??array?=?[NSArray?arrayWithObjects:@"a",?@"b",?@"c",?nil?nil];??NSArray?*array3?=?[array?arrayByAddingObjectsFromArray:[NSArray?arrayWithObjects:@"4",?@"5",?nil?nil]];????NSArray?*array4?=?[NSArray?arrayWithObjects:@"1",?@"2",?@"3",?@"4",?nil?nil];??NSRange?range?=?NSMakeRange(1,?2);??NSArray?*array5?=?[array4?subarrayWithRange:range];??
2.數組的一些基本方法
[objc] view plaincopy
int?count?=?[array?count];if?([array?containsObject:@"a"])?{??????NSLog(@"包含了字符串a");??}??NSString?*last?=?[array?lastObject];最后一個元素??NSString?*str?=?[array?objectAtIndex:1];根據索引獲取數組中的元素??int?index?=?[array?indexOfObject:@"c"];獲取指定元素的索引??NSArray?*array?=?[NSArray?arrayWithObjects:stu1,?stu2,?stu3,?nil?nil];??[array?makeObjectsPerformSelector:@selector(test2:)?withObject:@"123"];??NSArray?*array?=?[NSArray?arrayWithObjects:@"1",?@"2",?@"3",?@"4",?nil?nil];??NSString?*str?=?[array?componentsJoinedByString:@"-"];??NSString?*path?=?@"/Users/apple/Desktop/array.xml";??[array?writeToFile:path?atomically:YES];??path?=?@"/Users/apple/Desktop/array.txt";??NSArray?*array2?=?[NSArray?arrayWithContentsOfFile:path];?? 3.遍歷數組
[objc] view plaincopy
#pragma?mark?遍歷數組1??void?arrayFor1()?{??????NSArray?*array?=?[NSArray?arrayWithObjects:stu1,?@"1",?@"2",?@"3",?nil?nil];??????int?count?=?array.count;??????for?(int?i?=?0;?i<count;?i++)?{??????????id?obj?=?[array?objectAtIndex:i];??????}??}????#pragma?mark?遍歷數組2?快速遍歷??void?arrayFor2()?{??????Student?*stu1?=?[Student?student];??????NSArray?*array?=?[NSArray?arrayWithObjects:stu1,?@"1",?@"2",?@"3",?nil?nil];??????int?i?=0;??????for?(id?obj?in?array)?{??????????NSLog(@"%i-%@",?i,?obj);??????????i++;??????}??}????#pragma?mark?遍歷數組3??void?arrayFor3()?{??????Student?*stu1?=?[Student?student];??????NSArray?*array?=?[NSArray?arrayWithObjects:stu1,?@"1",?@"2",?@"3",?nil?nil];??????[array?enumerateObjectsUsingBlock:???????^(id?obj,?NSUInteger?idx,?BOOLBOOL?*stop)?{??????????NSLog(@"%i-%@",?idx,?obj);????????????????????if?(idx?==?1)?{????????????????????????????*stop?=?YES;???????????}??????}];??}????#pragma?mark?遍歷數組4??void?arrayFor4()?{??????Student?*stu1?=?[Student?student];??????NSArray?*array?=?[NSArray?arrayWithObjects:stu1,?@"1",?@"2",?@"3",?nil?nil];??????????????????NSEnumerator?*enumerator?=?[array?reverseObjectEnumerator];??????????NSArray?*array2?=?[enumerator?allObjects];??????NSLog(@"array2:%@",?array2);??????????id?obj?=?nil;??????while?(obj?=?[enumerator?nextObject])?{??????????NSLog(@"obj=%@",?obj);??????}??}??
4.數組排序
[objc] view plaincopy
#pragma?mark?數組排序1??void?arraySort1()?{??????NSArray?*array?=?[NSArray?arrayWithObjects:@"2",?@"3",?@"1",?@"4",?nil?nil];????????????????????NSArray?*array2?=?[array?sortedArrayUsingSelector:@selector(compare:)];??????NSLog(@"array2:%@",?array2);??}????#pragma?mark?數組排序2??void?arraySort2()?{??????Student?*stu1?=?[Student?studentWithFirstname:@"MingJie"?lastname:@"Li"];??????Student?*stu2?=?[Student?studentWithFirstname:@"LongHu"?lastname:@"Huang"];??????Student?*stu3?=?[Student?studentWithFirstname:@"LianJie"?lastname:@"Li"];??????Student?*stu4?=?[Student?studentWithFirstname:@"Jian"?lastname:@"Xiao"];??????NSArray?*array?=?[NSArray?arrayWithObjects:stu1,stu2,stu3,?stu4,?nil?nil];??????????NSArray?*array2?=?[array?sortedArrayUsingSelector:@selector(compareStudent:)];??????NSLog(@"array2:%@",?array2);??}??-?(NSComparisonResult)compareStudent:(Student?*)stu?{??????????NSComparisonResult?result?=?[self.lastname?compare:stu.lastname];??????????if?(result?==?NSOrderedSame)?{??????????result?=?[self.firstname?compare:stu.firstname];??????}??????return?result;??}????#pragma?mark?數組排序3??void?arraySort3()?{??????Student?*stu1?=?[Student?studentWithFirstname:@"MingJie"?lastname:@"Li"];??????Student?*stu2?=?[Student?studentWithFirstname:@"LongHu"?lastname:@"Huang"];??????Student?*stu3?=?[Student?studentWithFirstname:@"LianJie"?lastname:@"Li"];??????Student?*stu4?=?[Student?studentWithFirstname:@"Jian"?lastname:@"Xiao"];??????NSArray?*array?=?[NSArray?arrayWithObjects:stu1,stu2,stu3,?stu4,?nil?nil];????????????????NSArray?*array2?=?[array?sortedArrayUsingComparator:???????^NSComparisonResult(Student?*obj1,?Student?*obj2)?{????????????????????NSComparisonResult?result?=?[obj1.lastname?compare:obj2.lastname];????????????????????if?(result?==?NSOrderedSame)?{???????????????result?=?[obj1.firstname?compare:obj2.firstname];???????????}??????????????????????return?result;??????}];????????????NSLog(@"array2:%@",?array2);??}????#pragma?mark?數組排序4-高級排序??void?arraySort4()?{??????Student?*stu1?=?[Student?studentWithFirstname:@"MingJie"?lastname:@"Li"?bookName:@"book1"];??????Student?*stu2?=?[Student?studentWithFirstname:@"LongHu"?lastname:@"Huang"?bookName:@"book2"];??????Student?*stu3?=?[Student?studentWithFirstname:@"LianJie"?lastname:@"Li"?bookName:@"book2"];??????Student?*stu4?=?[Student?studentWithFirstname:@"Jian"?lastname:@"Xiao"?bookName:@"book1"];??????NSArray?*array?=?[NSArray?arrayWithObjects:stu1,stu2,stu3,?stu4,?nil?nil];????????????????????NSSortDescriptor?*bookNameDesc?=?[NSSortDescriptor?sortDescriptorWithKey:@"book.name"?ascending:YES];??????????NSSortDescriptor?*lastnameDesc?=?[NSSortDescriptor?sortDescriptorWithKey:@"lastname"?ascending:YES];??????????NSSortDescriptor?*firstnameDesc?=?[NSSortDescriptor?sortDescriptorWithKey:@"firstname"?ascending:YES];??????????NSArray?*descs?=?[NSArray?arrayWithObjects:bookNameDesc,?lastnameDesc,?firstnameDesc,?nil?nil];????????????NSArray?*array2?=?[array?sortedArrayUsingDescriptors:descs];????????????NSLog(@"array2:%@",?array2);??}??
轉載于:https://www.cnblogs.com/Free-Thinker/p/4972341.html
總結
以上是生活随笔為你收集整理的ios数组基本用法和排序的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。