NSArray
用来存储对象的有序列表,它是不可变的
不能存储C语言中的基本数据,如int、float、enum、struct、也不能存储nil。
一、#pragma mark 创建数组。
void arrayCreate() {
//创建一个空数组。
NSArray *array = [NSArray array];
//创建1个元素的数组
array = [NSArray arrayWithObject:@"123"];
//创建有多个元素的数组。
array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
NSLog(@"%@",array);
}
二、#pragma mark 数组的简单使用。
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(@"%@", str);
//查找某个元素的位置。
unsigned long index = [array indexOfObject:@"c"];
NSLog(@"%zi",index);
}
三、#pragma mark 数组的内存管理。
void arrayMemory() {
Student *stu1 = [[Student alloc] init];
Student *stu2 = [[Student alloc] init];
Student *stu3 = [[Student alloc] init];
//当用构造方法把一个对象塞进数组中时,这个对象的计数器会加1,也就是说数组会对它做一次retain操作。
NSArray *array = [[NSArray alloc] initWithObjects:stu1,stu2,stu3, nil];
//当用静态方法把对象塞进数组时,这个对象的计数器保持不变。
//NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];
NSLog(@"stu1:%zi", [stu1 retainCount]);
NSLog(@"count = %zi", array.count);
//数组被销毁,会对内部的所有元素都做一次release操作。
[array release];
[stu1 release];
[stu2 release];
[stu3 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];
NSLog(@"stu1:%zi", [stu1 retainCount]);
NSLog(@"count = %zi", array.count);
//让数组里的所有对象都调用test方法。
[array makeObjectsPerformSelector:@selector(test)];
//让数组里的所有对象都调用带一个参数的test1方法,并传入参数。
[array makeObjectsPerformSelector:@selector(test1:) withObject:@"123"];
}
五、#pragma mark 遍历数组
void arrayFor() {
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
//第一种方法。
unsigned long count = array.count;
for (int i = 0; i < count; i++) {
//id == void *
id obj = [array objectAtIndex:i];
NSLog(@"%i-%@",i,obj);
}
//第二种方法。
int i = 0;
for (id obj in array) {
NSLog(@"%i-%@",i,obj);
i++;
}
//第三种block方法。
[array enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop) {
NSLog(@"%zi-%@",idx,obj);
//如果索引为1,就停止遍历。
if(idx == 1) {
*stop = YES;
}
}];
//第四种,迭代器方式。
//获取数组的迭代器
NSEnumerator *enumerator = [array objectEnumerator];
//反序迭代器(从尾部开始遍历元素)。
NSEnumerator *enumerator1 = [array reverseObjectEnumerator];
//allObjects是取出没有被遍历过的对象。
//NSArray *array1 = [enumerator allObjects];
//NSLog(@"array1:%@",array1);
//获取下一个需要遍历的元素。
id obj = nil;
while (obj = [enumerator nextObject]) {
NSLog(@"obj = %@", obj);
};
id obj1 = nil;
while (obj1 = [enumerator1 nextObject]) {
NSLog(@"obj = %@", obj1);
};
}
六、#pragma mark 数组排序
void arraySort() {
NSArray *array = [NSArray arrayWithObjects:@"2",@"3",@"1",@"4", nil];
//返回一个排好序的数组,原来数组的元素顺序不会改变。
//指定元素的排序方法compare,从小到大排序。
NSArray *array1 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",array1);
//以学生的名字排序,先按姓排,如果姓相同则按名排。
Student *stu = [Student studentWithFristName:@"Ping" andLastName:@"Lan"];
Student *stu1 = [Student studentWithFristName:@"JiQuan" andLastName:@"Pu"];
Student *stu2 = [Student studentWithFristName:@"JueQi" andLastName:@"Zhang"];
Student *stu3 = [Student studentWithFristName:@"Chong" andLastName:@"Luo"];
Student *stu4 = [Student studentWithFristName:@"ZhongMing" andLastName:@"Zhang"];
Student *stu5 = [Student studentWithFristName:@"JueXing" andLastName:@"Zhang"];
NSArray *array2 = [NSArray arrayWithObjects:stu,stu1,stu2,stu3,stu4,stu5, nil];
//以compareStudent方法排序,compareStudent方法在Student类中声明和实现。以下是compareStudent方法。
- (NSComparisonResult)compareStudent:(Student *)stu {
//先按照姓排序
NSComparisonResult result = [self.lastName compare:stu.lastName];
//如果有相同的姓,就比较名字。
if (result == NSOrderedSame) {
result = [self.fristName compare:stu.fristName];
}
return result;
}
NSArray *array3 = [array2 sortedArrayUsingSelector:@selector(compareStudent:)];
//输出排序后的数组。
[array3 enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop) {
Student *stu = [[Student alloc] init];
stu = obj;
NSLog(@"%@%@",stu.lastName,stu.fristName);
}];
}
//用block对数组排序。
void ArraySortWithBlock() {
Student *stu = [Student studentWithFristName:@"Ping" andLastName:@"Lan"];
Student *stu1 = [Student studentWithFristName:@"JiQuan" andLastName:@"Pu"];
Student *stu2 = [Student studentWithFristName:@"JueQi" andLastName:@"Zhang"];
Student *stu3 = [Student studentWithFristName:@"Chong" andLastName:@"Luo"];
Student *stu4 = [Student studentWithFristName:@"ZhongMing" andLastName:@"Zhang"];
Student *stu5 = [Student studentWithFristName:@"JueXing" andLastName:@"Zhang"];
NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,stu3,stu4,stu5, nil];
//用block进行排序
NSArray *array1 = [array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
//先按照姓排序
NSComparisonResult result = [obj1.lastName compare:obj2.lastName];
//如果有相同的姓,就比较名字。
if (result == NSOrderedSame) {
result = [obj1.fristName compare:obj2.fristName];
}
return result;
}];
NSLog(@"array1:%@",array1);
}
#pragma mark 按照某种排序规则进行排序。
void arraySort1() {
Student *stu = [Student studentWithFristName:@"Ping" andLastName:@"Lan" andBookName:@"book1"];
Student *stu1 = [Student studentWithFristName:@"JiQuan" andLastName:@"Pu" andBookName:@"book2"];
Student *stu2 = [Student studentWithFristName:@"JueQi" andLastName:@"Zhang" andBookName:@"book1"];
Student *stu3 = [Student studentWithFristName:@"Chong" andLastName:@"Luo" andBookName:@"book4"];
Student *stu4 = [Student studentWithFristName:@"ZhongMing" andLastName:@"Zhang" andBookName:@"book3"];
Student *stu5 = [Student studentWithFristName:@"JueXing" andLastName:@"Zhang" andBookName:@"book4"];
NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,stu3,stu4,stu5, nil];
//先按书名进行排序
NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.bookName" ascending:YES];
//再按照姓进行排序
NSSortDescriptor *lastNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
//再按照名进行排序。
NSSortDescriptor *fristNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"fristName" ascending:YES];
//按顺序添加排序描述器。
NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastNameDesc, fristNameDesc, nil];
NSArray *array1 = [array sortedArrayUsingDescriptors:descs];
NSLog(@"%@",array1);
}
浙公网安备 33010602011771号