首页 > 代码库 > Objective-C基础笔记(9)Foundation常用类NSArray
Objective-C基础笔记(9)Foundation常用类NSArray
NSArray用来存储对象的有序列表,它是不可变的
NSArray不能存储C语言中的基本数据类型,如int、float、enum、struct,也不能存储nil,nil代表数组元素的结束
// // main.m // NSArray // // Created by lixiaoqiang on 14/11/19. // Copyright (c) 2014年 lixiaoqiang. All rights reserved. // #import <Foundation/Foundation.h> //数组的创建 void arrayCreate(){ //创建一个空的数组 NSArray *array = [NSArray array]; //创建有1个元素的数组 array = [NSArray arrayWithObject:@"123"]; //创建有多个元素的数组,nil代表元素的结束 array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil]; //查看元素个数 int count = [array count]; //或者使用[array.count] } //数组的简单使用 void arrayUse(){ NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil]; //判断是否包含了某个元素 if([array containsObject:@"a"]){ NSLog(@"包含了子元素a"); } //获取数组最后一个元素 NSString *last = [array lastObject]; NSLog(@"最后一个元素是%@", last); //获取某个索引处的元素 NSString *str = [array objectAtIndex:1]; NSLog(@"索引是1的位置是%@", str); //判读元素的索引 unsigned long index = [array indexOfObject:@"c"]; NSLog(@"c的位置索引是%zi", index); } //定义一个Student对象 @interface Student : NSObject //添加两个属性 @property (nonatomic, retain) NSString *firstname; @property (nonatomic, retain) NSString *lastname; + (id)student; //构造方法 + (id)studentWithFirstName:(NSString *)firstname lastname:(NSString *)lastname; - (void)test; //用于对象比较(返回值必须是NSComparisonResult类型) - (NSComparisonResult) compareStudent:(Student *)stu; @end @implementation Student + (id)student{ return [[[Student alloc] init] autorelease]; } + (id)studentWithFirstName:(NSString *)firstname lastname:(NSString *)lastname { Student *stu = [[[Student alloc] init] autorelease]; stu.firstname = firstname; stu.lastname = lastname; return stu; } - (void)test { NSLog(@"Student的test方法"); } - (NSComparisonResult)compareStudent:(Student *)stu{ //按照姓进行排序 return [self.lastname compare:stu.lastname]; } //相当于java的toString方法 - (NSString *)description{ return [NSString stringWithFormat:@"[%@ %@]", self.lastname, self.firstname]; } - (void)dealloc{ NSLog(@"%@被销毁了"); [_firstname release]; [_lastname release]; [super dealloc]; } @end //数组的内存管理 void arrayMemory(){ Student *stu1 = [[Student alloc] init]; Student *stu2 = [[Student alloc] init]; Student *stu3 = [[Student alloc] init]; NSLog(@"stu1:%zi", [stu1 retainCount]); //计数器为1 //数组会对添加的对象做一次retain操作 NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil]; NSLog(@"stu1:%zi", [stu1 retainCount]); //计数器为2 NSLog(@"count=%zi", array.count); //数组被销毁的时候会对内部的所有元素都做一次release操作 [array release]; [stu1 release]; [stu2 release]; [stu3 release]; } void arrayMessage(){ Student *stu1 = [Student student]; Student *stu2 = [Student student]; Student *stu3 = [Student student]; NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil]; //让数组里面的所有对象调用test方法 [array makeObjectsPerformSelector:@selector(test)]; } //遍历数组1 void arrayFor1(){ NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; //id == void * 代表任何类型的指针变量 int count = array.count; for(int i=0; i<count; i++){ id obj = [array objectAtIndex:i]; NSLog(@"%i位置是%@", i, obj); } } //遍历数组2 void arrayFor2(){ NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; for(id obj in array){ NSLog(@"%@", obj); } } //遍历数组3----Block void arrayFor3(){ NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%Zi位置是%@", idx, obj); if(idx == 1) *stop = YES; //如果索引为1则停止遍历 }]; } //遍历数组4----迭代器 void arrayFor4(){ NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; //获取数组的迭代器 NSEnumerator *enumerator = [array objectEnumerator]; //反向迭代器 //NSEnumerator *renu = [array reverseObjectEnumerator]; //获取下一个遍历的元素 //[enumerator nextObject]; id obj = nil; while(obj = [enumerator nextObject]){ NSLog(@"%@", obj); } } //派生出新的数组 void arrayNew(){ //添加元素 NSArray *array = [NSArray arrayWithObjects:@"1", @"2", nil]; NSArray *array2 = [array arrayByAddingObject:@"3"]; //裁剪元素 NSRange range = NSMakeRange(1, 2); [array2 subarrayWithRange:range]; } //数组的其他用法 void arrayOther(){ //利用分隔符拼接 NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil]; NSString *str = [array componentsJoinedByString:@","]; NSLog(@"%@", str); //1,2,3,4 //数组写入文件 NSArray *path = @"/user/apple/Desktop/array.xml"; [array writeToFile:path atomically:YES]; //从文件读出数组 NSArray *array2 = [NSArray arrayWithContentsOfFile:path]; } //数组排序1 void arraySort1(){ NSArray *array = [NSArray arrayWithObjects:@"3", @"1", @"4", @"2", nil]; //指定元素的比较方法(compare:) NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"array2:%@", array2); } //数组排序2 void arraySort2(){ Student *stu1 = [Student studentWithFirstName:@"sun" lastname:@"home"]; Student *stu2 = [Student studentWithFirstName:@"dawan" lastname:@"ganban"]; Student *stu3 = [Student studentWithFirstName:@"lxq" lastname:@"xsyu"]; NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil]; NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)]; } //数组排序3---Block排序 void arraySort3(){ Student *stu1 = [Student studentWithFirstName:@"sun" lastname:@"home"]; Student *stu2 = [Student studentWithFirstName:@"dawan" lastname:@"ganban"]; Student *stu3 = [Student studentWithFirstName:@"lxq" lastname:@"xsyu"]; NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil]; [array sortedArrayUsingComparator:^NSComparisonResult(Student *stu1, Student *stu2) { return [stu1.lastname compare:stu2.lastname]; }]; } //数组排序4,可以用来对对象嵌套排序 比如Student中有一个book对象就可以使用@"book.bookname"对书名排序 void arraySort4(){ Student *stu1 = [Student studentWithFirstName:@"sun" lastname:@"home"]; Student *stu2 = [Student studentWithFirstName:@"dawan" lastname:@"ganban"]; Student *stu3 = [Student studentWithFirstName:@"lxq" lastname:@"xsyu"]; //创建排序描述器 NSSortDescriptor *descript = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES]; NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil]; //调用排序 NSArray *array2 = [array sortedArrayUsingDescriptors:descript]; } int main(int argc, const char * argv[]) { @autoreleasepool { //arrayFor4(); } return 0; }
NSMutableArray是可变数组,派生自NSArray,相关操作如下
void arrayCreate(){ NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1", @"2", nil]; [array addObject:@"3"]; [array removeObject:@"3"]; [array removeLastObject]; [array removeAllObjects]; } void arrayMemory(){ //对被添加的元素做一次retain操作 //[array addObject:stu1]; //对被删除的元素做一次release操作 //[array removeObject:stu1]; //当数组被释放的时候会对所有的元素做一次release操作 //[array release]; }
Objective-C基础笔记(9)Foundation常用类NSArray
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。