Objective-C中NSArray的基本用法示例
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];
// 指定系統(tǒng)自帶規(guī)定的比較方法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,自己定義比較方式,但是一定要實(shí)現(xiàn)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.先按照書名進(jìn)行排序
NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
// 2.先按照姓進(jìn)行排序
NSSortDescriptor *firstNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
// 3.先按照名進(jìn)行排序
NSSortDescriptor *lastNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
NSArray *array2 = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:bookNameDesc,firstNameDesc,lastNameDesc, nil nil]];
NSLog(@"%@",array2);
}
NSArray的一些用法
NSArray 只允許裝OC對象,并且不能裝空值,空代表數(shù)組元素的結(jié)束
#pragma mark - NSArray的基本用法
// 創(chuàng)建一個(gè)空數(shù)組
NSArray *array = [NSArray array];
// 創(chuàng)建有一個(gè)元素的數(shù)組
array = [NSArray arrayWithObject:@"123"];
// 創(chuàng)建有多個(gè)元素的數(shù)組
array = [NSArray arrayWIthObjects:@"a",@"b",nil ];//不能裝nil空指針,空值代表數(shù)組元素結(jié)束
// 將一個(gè)數(shù)組賦值給一個(gè)數(shù)組
+ (instancetype)arrayWithArray:(NSArray *)array;
// 獲取元素的個(gè)數(shù)
int count = [array count]; //和 count = array.count; 相同,都是調(diào)用get方法
// 是否包含一個(gè)元素
- (bool)containsObject:(id)anObject;
// 返回最后一個(gè)元素
- (id) lastObject;
// 獲取index位置的元素
- (id)objectAtIndex:(NSUInteger) index;
// 獲取元素的位置
- (NSUInteger) indexOfObject:(id)anObject;
// 在range范圍內(nèi)查找元素的位置
- (NSUInteger) indexofObject:(id)anObject inRange:(NSRange)range;
// 比較兩個(gè)集合內(nèi)容是否相同
- (Bool) isEqualToArray:(NSArray *)otherArray;
// 返回兩個(gè)集合中第一個(gè)相同的對象元素
- (id) firstObjectCommonWithArray:(NSArray *)otherArray;
#pragma mark - NSArray的高級用法
//讓集合里面的所有元素都執(zhí)行aSelector這個(gè)方法
- (void)makeObjectsPerformSelector:(SEL)aSelector;
//讓集合里面的所有元素都執(zhí)行aSelector這個(gè)方法,給這個(gè)方法添加參數(shù),但是只支持一個(gè)參數(shù)
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument
//添加一個(gè)元素,返回一個(gè)新的NSArray(方法調(diào)用者本身沒有發(fā)生變化)
- (NSArray *)arrayByAddingObject:(id)anObject
//添加otherArray的所有元素,返回一個(gè)新的NSArray(方法著本身沒有改變)
- (NSArray *) arrayByAddingObjectsFromArray:(NSArray *) otherArray;
//截取range范圍的數(shù)組
- (NSArray *) subarrayWithRange:(NSRenge)range;
//用separator做拼接符,拼接成一個(gè)字符串
- (NSString *) componentsJoinedByString:(NSString *)separator
//將NSArray持久化到文件中去
- (Bool) writeToFile:(NSString *)path atomically:(Bool)useAuxiliaryFile
#pragma mark - NSArray的遍歷
// 方法一:普通遍歷(利用for循環(huán))
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 有相應(yīng)的方法來獲取迭代器
//獲取一個(gè)正序遍歷的迭代器
- (NSEnumerator *) objectEnumerator;
//獲取一個(gè)反序遍歷的迭代器
- (NSEnumerator *) reverseObjectEnumerator;
@常用方法:
//獲取下一個(gè)元素
- (id) nextObject;
//獲取所有的元素
- (NSArray *) allObjects
void arrayFor4(){
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
NSEnumerator *enumerator = [array objectEnumerator];// 返回?cái)?shù)組的迭代器
//如果放到遍歷之后,則取到空,原因是,遍歷完了,就沒值了
NSArray *array2 = [enumerator allObjects];
NSLog(@"array2=%@", array2);
//獲取下一個(gè)需要遍歷的元素
id obj = nil;
while (obj = [enumerator nextObject]) {
NSLog(@"obj=%@", obj);
}
}
使用block 塊遍歷整個(gè)數(shù)組。這個(gè)block 需要三個(gè)參數(shù),id obj 表示數(shù)組中的元素。
NSUInteger idx 標(biāo)示元素的下標(biāo),
boolbool *stop 是一個(gè)bool類型的參數(shù)。 官方描述如下:
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
調(diào)用例子如:
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test", nil nil];
[array enumerateObjectsUsingBlock:^(id str,NSUInteger index, BOOL* te){
NSLog(@"%@,%d",str,index);
}];
同上面的方法一項(xiàng),區(qū)別在于,這里多添加了一個(gè)參數(shù),用來標(biāo)示 是從前向后遍歷,還是從后往前遍歷。
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx,BOOLBOOL *stop))block
調(diào)用例子如下:
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test", nil nil];
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id str,NSUInteger index, BOOL* te){
NSLog(@"%@,%d",str,index);
}];
- 在一個(gè)項(xiàng)目中同時(shí)使用Swift和Objective-C代碼混合編程的方法
- objective-c中生成隨機(jī)數(shù)的方法
- Swift調(diào)用Objective-C編寫的API實(shí)例
- Objective-C 消息傳遞機(jī)制詳解
- Objective-c代碼如何移植為Swift代碼 Objective-c代碼轉(zhuǎn)移到Swift過程介紹
- Swift能代替Objective-C嗎?
- 在Swift中使用Objective-C編寫類、繼承Objective-C類
- 全面解析Objective-C中的block代碼塊的使用
- Objective-C中NSLog輸出格式大全
- 使用Objective-C獲取IPHONE手機(jī)IMSI序列號(hào)
- Swift調(diào)用Objective-C代碼
- Objective-C中的重載和重寫詳解
相關(guān)文章
iOS項(xiàng)目的開發(fā)命名規(guī)范教程
為了團(tuán)隊(duì)各成員之間代碼的互通、可讀、易維護(hù)性,特制訂此開發(fā)規(guī)范。下面這篇文章主要給大家介紹了關(guān)于iOS項(xiàng)目的開發(fā)命名規(guī)范的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11Flutter?模型動(dòng)態(tài)化賦值研究分析
這篇文章主要為大家介紹了Flutter?模型動(dòng)態(tài)化賦值研究分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03解決蘋果ios用js的Date()出現(xiàn)NaN的問題
下面小編就為大家分享一篇解決蘋果ios用js的Date()出現(xiàn)NaN的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03