NSArray是用来存储对象的有序列表(NSSet是没有顺序的),它是不可变的。NSArray不能存储C语言中的基本数据类型,如int\float\enum\struct等,也不能存储nil。其用法如下:

 #pragma mark 创建一个数组
void arrayCreate() {
// 创建一个空的数组
NSArray *array = [NSArray array]; // 创建有1个元素的数组
array = [NSArray arrayWithObject:@""]; // 创建有多个元素的数组
array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil]; int count = [array count];
// count = array.count;
NSLog(@"%i", count);
} #pragma mark 数组的简单使用
void arrayUse() {
NSObject *obj = [[NSObject alloc] init];
NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c" , obj, nil];
// 判断是否包含了某个元素
if ([array containsObject:@"a"]) {
NSLog(@"包含了字符串a");
} NSString *last = [array lastObject];
NSLog(@"last=%@", last); NSString *str = [array objectAtIndex:];
NSLog(@"%@", str); int index = [array indexOfObject:@"c"];
NSLog(@"index=%i", index); [obj release];
} #pragma mark 数组的内存管理
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]); NSLog(@"count=%zi", array.count); //
[stu1 release];
[stu2 release];
[stu3 release]; // 数组被销毁的时候,会对内部的所有元素都做一次release操作
//
[array release];
} #pragma mark 给数组里面的元素发送消息
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)];
[array makeObjectsPerformSelector:@selector(test2:) withObject:@""];
} #pragma mark 遍历数组1
void arrayFor1() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"", @"", @"", nil];
int count = array.count;
for (int i = ; i<count; i++) {
// id == void *
id obj = [array objectAtIndex:i];
NSLog(@"%i-%@", i, obj);
}
} #pragma mark 遍历数组2
void arrayFor2() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"", @"", @"", nil];
// 快速遍历
int i =;
for (id obj in array) {
NSLog(@"%i-%@", i, obj);
i++;
}
} #pragma mark 遍历数组3
void arrayFor3() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"", @"", @"", nil];
[array enumerateObjectsUsingBlock:
^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%i-%@", idx, obj); // 如果索引为1,就停止遍历
if (idx == ) {
// 利用指针修改外面BOOL变量的值
*stop = YES;
}
}];
} #pragma mark 遍历数组4
void arrayFor4() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"", @"", @"", nil]; // 获取数组的迭代器
// NSEnumerator *enumerator = [array objectEnumerator];
// 反序迭代器(从尾部开始遍历元素)
NSEnumerator *enumerator = [array reverseObjectEnumerator]; // allObjects是取出没有被遍历过的对象
NSArray *array2 = [enumerator allObjects];
NSLog(@"array2:%@", array2); // 获取下一个需要遍历的元素
id obj = nil;
while (obj = [enumerator nextObject]) {
NSLog(@"obj=%@", obj);
}
}
#import <Foundation/Foundation.h>

@interface Book : NSObject
@property (nonatomic, retain) NSString *name; + (id)bookWithName:(NSString *)name;
@end #import "Book.h" @implementation Book + (id)bookWithName:(NSString *)name {
Book *book = [[[Book alloc] init] autorelease];
book.name = name;
return book;
} - (void)dealloc {
[_name release];
[super dealloc];
}
@end
 #pragma mark 派生出新的数组
void arrayNew() {
NSArray *array = [NSArray arrayWithObjects:@"", @"", nil]; NSArray *array2 = [array arrayByAddingObject:@""]; NSArray *array3 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"", @"", nil]]; NSLog(@"array:%@", array);
NSLog(@"array2:%@", array2);
NSLog(@"array3:%@", array3); NSArray *array4 = [NSArray arrayWithObjects:@"", @"", @"", @"", nil];
NSRange range = NSMakeRange(, );
NSArray *array5 = [array4 subarrayWithRange:range];
NSLog(@"array5:%@", array5);
} #pragma mark 数组的其他用法
void arrayOther() {
NSArray *array = [NSArray arrayWithObjects:@"", @"", @"", @"", nil];
// 1-2-3-4
// 利用分隔符-拼接所有的数组元素
NSString *str = [array componentsJoinedByString:@"-"];
NSLog(@"%@", str); // 将一个数组写入文件(生成的是一个xml文件)
NSString *path = @"/Users/apple/Desktop/array.xml";
[array writeToFile:path atomically:YES]; path = @"/Users/apple/Desktop/array.txt";
// 从文件中读取数组内容(文件有严格的格式要求)
NSArray *array2 = [NSArray arrayWithContentsOfFile:path];
NSLog(@"array2:%@", array2);
} #pragma mark 数组排序1
void arraySort1() {
NSArray *array = [NSArray arrayWithObjects:@"", @"", @"", @"", nil]; // 返回一个排好序的数组,原来数组的元素顺序不会改变
// 指定元素的比较方法:compare:
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]; // 指定排序的比较方法
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)]; NSLog(@"array2:%@", array2);
} #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]; // 利用block进行排序
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]; // 1.先按照书名进行排序
// 这里的key写的是@property的名称
NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
// 2.再按照姓进行排序
NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
// 3.再按照名进行排序
NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
// 按顺序添加排序描述器
NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil]; NSArray *array2 = [array sortedArrayUsingDescriptors:descs]; NSLog(@"array2:%@", array2);
}

Objective-C:Foundation框架-常用类-NSArray的更多相关文章

  1. Objective-C:Foundation框架-常用类-NSString全解

    Foundation框架中常用的类有字符串.集合.字典等,这里介绍字符串NSString.本文分别介绍了NSString的创建.从文件里读取NSString字符串.通过函数改变外部的NSString变 ...

  2. Objective-C:Foundation框架-常用类-NSDictionary

    与NSString.NSArray一样,NSDictionary是不可变的,其对应可变类型为NSMutableDictionary.其用法如下: #import <Foundation/Foun ...

  3. Objective-C:Foundation框架-常用类-NSObject

    NSObject是所有类的基类,其常见用法有: #import <Foundation/Foundation.h> @interface Person : NSObject - (void ...

  4. Objective-C:Foundation框架-常用类-NSNumber

    NSArray.NSDictionary是不可以存储C语言中的基本数据类型的.NSNumber可以将基本数据类型包装成对象,这样可以间接将基本数据类型存进NSArray.NSDictionary等集合 ...

  5. Objective-C:Foundation框架-常用类-NSMutableDictionary

    直接上代码吧: #import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic, ...

  6. Objective-C:Foundation框架-常用类-NSMutableArray

    NSMutableArray是NSArray对的子类,它是可变的,可以随意添加或者删除元素.与Array,也有静态和动态两种创建方法.也可已使用NSArray的方法来创建NSMutableArray. ...

  7. Objective-C:Foundation框架-常用类-NSDate

    直接上代码吧: #import <Foundation/Foundation.h> #pragma mark 日期创建 void dateCreate() { // date方法返回的就是 ...

  8. Objective-C:Foundation框架-常用类-NSNull

    集合中是不能存储nil值的,因为nil在集合中有特殊含义,但有时确实需要存储一个表示“什么都没有”的值,那么可以使用NSNull,它也是NSObject的一个子类. #import <Found ...

  9. Objective-C:Foundation框架-常用类-NSValue

    NSNumber是NSValue的子类,前者只能包装数字,后者可以包装任意值.NSArray.NSDictionary只能存储OC对象,不能存储结构体.因此,如果想要在NSArray.NSDictio ...

随机推荐

  1. SAP传输系统TMS、开发类、传输请求

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. bzoj 1208: [HNOI2004]宠物收养所 set

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7328  Solved: 2892[Submit][Sta ...

  3. CI实践_Android持续集成

    之前已经实现了Android的持续集成,并在项目中应用了一段时间.恰逢现在有几分钟时间,把之前的一些零散的点滴记录和整理一下,供有需要的朋友参考,或后续复用. 需要的准备知识:gitlab.Jenki ...

  4. Iframe刷新父窗口的几种方式

    /*Iframe刷新父窗口的几种方式在iframe的子页面中,使用onload刷新父页面的时候,遇到了一些问题. 1.目前来说,测试成功,并且兼容IE6/7和FF的刷新方式. */ <scrip ...

  5. spring的下载地址(转)

    http://hi.baidu.com/jaimy_jie/item/d699cecdc5d3a354bdef6990 SPRING官方网站改版后,建议都是通过 Maven和Gradle下载,对不使用 ...

  6. fFFmpeg 命令、案例、测试集中营

    gitbook: https://www.gitbook.com/book/xdsnet/other-doc-cn-ffmpeg/details ffmpeg [全局选项] {[输入文件选项] -i ...

  7. 图片在父元素中上下居中(vertical-align的有效性)

    在实际的使用中,会遇到img上下居中的问题: 1.一般情况下,将其放置在table中:可以自动的上下居中.   2.另外一种情况<即一般情况下> 以li中为例子:在无序列表中 li元素下的 ...

  8. KMPlayer

    1. KMP 播放时,有声无图像,黑屏.解决: 打开KMP然后右键-选项-参数设置(或者进入KMP直接按F2)-视频处理-右边选择“渲染器”-在“渲染器”中选择“VMR9 未渲染 (HQ字幕)”-选中 ...

  9. Oracle 遇到的错误及处理整理 - 记录

    1. 启动监听提示: TNS-: TNS:permission denied TNS-: TNS:protocol adapter error TNS-: Insufficient privilege ...

  10. SDL2.0的SDL_Event事件处理

    SDL_Event事件集合 SDL_AudioDeviceEvent SDL_ControllerAxisEvent SDL_ControllerButtonEvent SDL_ControllerD ...