NSArray的排序
+ (id)studentWithFirstName:(NSString *)firstName lastName:(NSString *)lastName{
Student *stu = [[Student alloc] init];
stu.firstName = firstName;
stu.lastName = lastName;
return stu;
}
+ (id)studentWithFirstName:(NSString *)firstName lastName:(NSString *)lastName bookName:(NSString *)bookName{
Student *stu = [Student studentWithFirstName:firstName lastName:lastName];
stu.book = [Book bookWithName:bookName];
return stu;
}
- (NSComparisonResult)compareStudent:(Student *)stu{
NSComparisonResult result = [self.firstName compare:stu.firstName];
if (result == NSOrderedSame) {
result = [self.lastName compare:stu.lastName];
}
return result;
}
- (NSString *)description{
//return [NSString stringWithFormat:@" %@ %@ %@",self.firstName,self.lastName,self.book.name];
return [NSString stringWithFormat:@" %@ %@ %@",self.firstName,self.lastName,_book.name];
}
#pragma mark 3.NSArray排序1
void arraySort1(){
NSArray *array = [NSArray arrayWithObjects:@"2",@"3",@"1",@"4", nil nil];
// 指定體系自帶劃定的比擬辦法compare:
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",array2);
}
#pragma mark 3.NSArray排序2
void arraySort2(){
Student *stu1 = [Student studentWithFirstName:@"hu" lastName:@"mingtao"];
Student *stu2 = [Student studentWithFirstName:@"zhu" lastName:@"wenpeng"];
Student *stu3 = [Student studentWithFirstName:@"zhao" lastName:@"weisong"];
Student *stu4 = [Student studentWithFirstName:@"hu" lastName:@"junpeng"];
NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil nil];
// 相似JAVA中得compareTo,本身界說比擬方法,然則必定要完成compare辦法
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"%@",array2);
}
#pragma mark 3.NSArray排序3-Block排序
void arraySort3(){
Student *stu1 = [Student studentWithFirstName:@"hu" lastName:@"mingtao"];
Student *stu2 = [Student studentWithFirstName:@"zhu" lastName:@"wenpeng"];
Student *stu3 = [Student studentWithFirstName:@"zhao" lastName:@"weisong"];
Student *stu4 = [Student studentWithFirstName:@"hu" lastName:@"junpeng"];
NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil nil];
NSArray *array2 = [array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
NSComparisonResult result = [obj1.firstName compare:obj2.firstName];
if (result == NSOrderedSame) {
result = [obj1.lastName compare:obj2.lastName];
}
return result;
}];
NSLog(@"%@",array2);
}
#pragma mark 4.NSArray排序4-高等排序
void arraySort4(){
Student *stu1 = [Student studentWithFirstName:@"hu" lastName:@"mingtao" bookName:@"lianai"];
Student *stu2 = [Student studentWithFirstName:@"zhu" lastName:@"wenpeng" bookName:@"tianshi"];
Student *stu3 = [Student studentWithFirstName:@"zhao" lastName:@"weisong" bookName:@"love"];
Student *stu4 = [Student studentWithFirstName:@"hu" lastName:@"junpeng" bookName:@"qingren"];
NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil nil];
// 1.先依照書名停止排序
NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
// 2.先依照姓停止排序
NSSortDescriptor *firstNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
// 3.先依照名停止排序
NSSortDescriptor *lastNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
NSArray *array2 = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:bookNameDesc,firstNameDesc,lastNameDesc, nil nil]];
NSLog(@"%@",array2);
}
NSArray的一些用法
NSArray 只許可裝OC對象,而且不克不及裝空值,空代表數組元素的停止
#pragma mark - NSArray的根本用法
// 創立一個空數組
NSArray *array = [NSArray array];
// 創立有一個元素的數組
array = [NSArray arrayWithObject:@"123"];
// 創立有多個元素的數組
array = [NSArray arrayWIthObjects:@"a",@"b",nil ];//不克不及裝nil空指針,空值代表數組元素停止
// 將一個數組賦值給一個數組
+ (instancetype)arrayWithArray:(NSArray *)array;
// 獲得元素的個數
int count = [array count]; //和 count = array.count; 雷同,都是挪用get辦法
// 能否包括一個元素
- (bool)containsObject:(id)anObject;
// 前往最初一個元素
- (id) lastObject;
// 獲得index地位的元素
- (id)objectAtIndex:(NSUInteger) index;
// 獲得元素的地位
- (NSUInteger) indexOfObject:(id)anObject;
// 在range規模外調找元素的地位
- (NSUInteger) indexofObject:(id)anObject inRange:(NSRange)range;
// 比擬兩個聚集內容能否雷同
- (Bool) isEqualToArray:(NSArray *)otherArray;
// 前往兩個聚集中第一個雷同的對象元素
- (id) firstObjectCommonWithArray:(NSArray *)otherArray;
#pragma mark - NSArray的高等用法
//讓聚集外面的一切元素都履行aSelector這個辦法
- (void)makeObjectsPerformSelector:(SEL)aSelector;
//讓聚集外面的一切元素都履行aSelector這個辦法,給這個辦法添加參數,然則只支撐一個參數
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(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 *)path atomically:(Bool)useAuxiliaryFile
#pragma mark - NSArray的遍歷
// 辦法一:通俗遍歷(應用for輪回)
void arrayFor1(){
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
int count = array.count;
for(int i=0; i<count; i++){
id obj = [array objectAtIndex:i];
NSLog(@"%i-%@",i, obj);
}
}
// 辦法二:疾速遍歷
void arrayFor2(){
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
int count = array.count;
int i=0;
for(id obj in array){
NSLog(@"%i-%@",i, obj);
i++;
}
}
// 辦法三:應用block遍歷
void arrayFor3(){
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%zi->%@",idx, obj);
// *stop = YES; //轉變外邊的Bool,終止遍歷
}];
}
// 辦法四:應用迭代器
先引見一下-->NSEnumerator迭代器:聚集的迭代器,可以用於遍歷聚集元素,NSArray 有響應的辦法來獲得迭代器
//獲得一個正序遍歷的迭代器
- (NSEnumerator *) objectEnumerator;
//獲得一個反序遍歷的迭代器
- (NSEnumerator *) reverSEObjectEnumerator;
@經常使用辦法:
//獲得下一個元素
- (id) nextObject;
//獲得一切的元素
- (NSArray *) allObjects
void arrayFor4(){
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
NSEnumerator *enumerator = [array objectEnumerator];// 前往數組的迭代器
//假如放到遍歷以後,則取到空,緣由是,遍歷完了,就沒值了
NSArray *array2 = [enumerator allObjects];
NSLog(@"array2=%@", array2);
//獲得下一個須要遍歷的元素
id obj = nil;
while (obj = [enumerator nextObject]) {
NSLog(@"obj=%@", obj);
}
}
應用block 塊遍歷全部數組。這個block 須要三個參數,id obj 表現數組中的元素。
NSUInteger idx 標示元素的下標,
boolbool *stop 是一個bool類型的參數。 官方描寫以下:
A reference to a Boolean value. The block can set the value to YES to stop further processing of the array.
The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx,BOOLBOOL *stop))block
挪用例子如:
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test", nil nil];
[array enumerateObjectsUsingBlock:^(id str,NSUInteger index, BOOL* te){
NSLog(@"%@,%d",str,index);
}];
同下面的辦法一項,差別在於,這裡多添加了一個參數,用來標示 是早年向後遍歷,照樣從後往前遍歷。
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx,BOOLBOOL *stop))block
挪用例子以下:
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test", nil nil];
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id str,NSUInteger index, BOOL* te){
NSLog(@"%@,%d",str,index);
}];
【Objective-C中NSArray的根本用法示例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!