NSArray用来存储对象的有序列表,可以放入任何类型的对象,但它有两个限制:首先,它只能存储OC的对象,不能存储C中的基本数据类型,如 int , float , enum , struct 和随机指针 ,其次不能在NSArray中存储nil。
创建数组
NSArray *array1 = @[@"一年级",@"二年级",@"三年级",@"四年级",@"五年级",@"六年级"];//数组的元素可以使用任意的对象,//数组中装的是元素的地址
NSLog(@"%@",array1);//打印数组就是打印数组的每个元素(description方法的返回值)
一些常用方法
NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c",nil];
//判断是否包含某个元素
if ([array containsObject:@"a"])
{
NSLog(@"包含了字符串a");
}
//返回最后一个元素
NSString *last = [array lastObject];
NSLog(@"last = %@",last);
//给数组里的元素发消息
Student *stu1 = [Student student];
Student *stu2 = [Student student];
Student *stu3 = [Student student];
NSArray *array1 = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];
//数组里的所有对象都调用test方法
[array1 makeObjectsPerformSelector:@selector(test)];
//给test1:方法传递参数
[array1 makeObjectsPerformSelector:@selector(test1:) withObject:@"123"];
NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c",nil];
//在旧数组基础上添加一个新元素返回一个新的数组(方法调用者本身没有改变)
NSArray *array2 = [array arrayByAddingObject:@"12"];
NSLog(@"%@",array);
NSLog(@"%@",array2);
//添加一个数组的所有元素,返回一个新的NSArray(方法调用者本身也没有改变)
NSArray *array3 = [array arrayByAddingObjectsFromArray:@[@"4",@"5"]];
NSLog(@"%@",array3);
//获取range范围内的数组元素返回一个新数组
NSArray *array5 = [array3 subarrayWithRange:NSMakeRange(2, 3)];
NSLog(@"array5 = %@",array5);
//将一个数组写入文件
NSString *path = @"/Users/lijun/Desktop/array.txt";
[array writeToFile:path atomically:YES];
//从文件中读取数组内容
NSArray *array6 = [NSArray arrayWithContentsOfFile:path];
NSLog(@"array6 = %@",array6);
- (NSUInteger)indexOfObject:(id)anObject;//查找anObject元素在数组中的位置
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;//在range范围内查找anObject元素在数组中的位置
数组的遍历
一、枚举器法
NSEnumerator *enumerator = [array1 objectEnumerator];
// allObjects是取出没有被遍历过的对象
NSArray *array2 = [enumerator allObjects];
NSLog(@"%@",array2);
id objc;
//当没有下一个需要遍历的元素时循环退出
while (objc = [enumerator nextObject])
{
NSLog(@"%@",objc);
}
NSEnumerator *enumerator = [array1 reverseObjectEnumerator];//逆序遍历
id objc;
while (objc = [enumerator nextObject])
{
NSLog(@"%@",objc);
}
二、快速枚举法
for (id objc in array1)
{
NSLog(@"%@",objc);
}
三、使用 i值遍历
NSUInteger length = [array1 count];
NSInteger i;
for (i = 0; i < length; i++)
{
NSLog(@"%@",[array1 objectAtIndex:i]);
}
四、使用block遍历
[array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@ %zi",obj,idx);
//当索引为1时,终止遍历
if (idx == 1)
{
*stop = YES;
}
}];
数组排序
void arraySort1()
{
NSArray *array = [NSArray arrayWithObjects:@"2",@"3",@"1",@"4", nil];
//返回一个排好序的数组,原来数组的元素顺序不会改变
//指定元素的比较方法:compare:
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"array2 = %@",array2);
}
void arraySort2()
{
Student *stu1 = [Student studentWithFirstName:@"MingBo" lastName:@"Li"];
Student *stu2 = [Student studentWithFirstName:@"ShiMing" lastName:@"Li"];
Student *stu3 = [Student studentWithFirstName:@"Feng" lastName:@"Xiao"];
Student *stu4 = [Student studentWithFirstName:@"Zhu" lastName:@"Duan"];
NSArray *array = @[stu1,stu2,stu3,stu4];
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"array2 = %@",array2);
}
-(NSComparisonResult)compareStudent:(Student *)stu
{
NSComparisonResult result = [self.lastName compare:stu.lastName];
if (result == NSOrderedSame)
{
result = [self.firstName compare:stu.firstName];
}
return result;
}
//利用block进行排序
void arraySort3()
{
Student *stu1 = [Student studentWithFirstName:@"MingBo" lastName:@"Li"];
Student *stu2 = [Student studentWithFirstName:@"ShiMing" lastName:@"Li"];
Student *stu3 = [Student studentWithFirstName:@"Feng" lastName:@"Xiao"];
Student *stu4 = [Student studentWithFirstName:@"Zhu" lastName:@"Duan"];
NSArray *array = @[stu1,stu2,stu3,stu4];
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);
}
void arraySort4()
{
Student *stu1 = [Student studentWithFirstName:@"MingBo" lastName:@"Li" bookName:@"book1"];
Student *stu2 = [Student studentWithFirstName:@"ShiMing" lastName:@"Li" bookName:@"book3"];
Student *stu3 = [Student studentWithFirstName:@"Feng" lastName:@"Xiao" bookName:@"book2"];
Student *stu4 = [Student studentWithFirstName:@"Zhu" lastName:@"Duan" bookName:@"book1"];
NSArray *array = @[stu1,stu2,stu3,stu4];
//按照书名进行排序
NSSortDescriptor *bookName = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
//按照姓进行排序
NSSortDescriptor *last = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
//按照名进行排序
NSSortDescriptor *first = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
//按顺序添加排序描述器 (bookName排在第一位表示先按照书名排序)
NSArray *descs = @[bookName,last,first];
NSArray *array2 = [array sortedArrayUsingDescriptors:descs];
NSLog(@"array = %@",array2);
}