您的位置:首页 > 其它

使用对象集合(通过Foundation框架使用OC处理数组)

2016-01-18 11:52 465 查看
在OC中有三种对象集合:数组、字典与集合。到底使用哪种取决于应用的需要。

1.引用数组中的对象

NSString *stringObject1 = [listOfLetters2 objectAtIndex:0];

NSString *stringObject2 = [listOfLetters2 lastObject];

NSUInteger position = [listOfLetters2 indexOfObject:@"2"];


2.获取数组中元素的数量

NSArray对象提供了count属性,可以通过这个属性获得数组中元素的数量

3.遍历数组

// NSArray对象提供了三种内置方式来遍历对象列表。很多人使用for-each循环遍历数组中的每个元素。通过这种结构,可以使用相同的代码来遍历数组中的每个元素。

// 还可以使用makeObjectsPerformSelector:方法,在这种情况下,可以传递希望每个对象都执行的方法名和一个参数

// 还可以使用enumerateObjectsUsingBlock:方法将代码块作为参数应用到数组中的每个对象上。该方法的作用与for-each循环一样,但无须为循环本身编写代码,并且可以通过参数追踪当前元素的索引

NSMutableString *string1 = [NSMutableString stringWithString:@"A"];
NSMutableString *string2 = [NSMutableString stringWithString:@"B"];
NSMutableString *string3 = [NSMutableString stringWithString:@"C"];

NSArray *listOfObjects = @[string1,string2,string3];

for (NSMutableString *s in listOfObjects) {

// lowercaseString 以小写方式返回字符串
NSLog(@"This string in lowercase is %@",[s lowercaseString]);
}

[listOfObjects makeObjectsPerformSelector:@selector(appendString:) withObject:@"-MORE"];

// 块是一种代码封装方式,这样一来,代码就可以被当做对象,并以参数形式传递给其他对象。借助于NSArray方法enumerateObjectsUsingBlock:,就可以对数组中的每个元素执行代码块;
[listOfObjects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"object(%lu)'s description is %@",idx, [obj description]);
}];


4.排序数组

// 为用于数组排序的每个属性创建NSSortDescriptor对象。将所有这些NSSortDescriptor对象放到一个数组中,该数组将会在后面用做参数。使用NSArray类的sortedArrayUsingDescriptors:方法并将NSSortDescriptor对象数组作为参数传递进去,结果会返回一个数组,这个数组中的对象已根据你置顶的属性排好序。

// 创建Person对象数组
Person *p1 = [[Person alloc] initWithFirstName:@"Rebecca"
lastName:@"Smith"
andAge:33];
Person *p2 = [[Person alloc] initWithFirstName:@"Albert"
lastName:@"Case"
andAge:24];
Person *p3 = [[Person alloc] initWithFirstName:@"Anton"
lastName:@"Belfey"
andAge:45];
Person *p4 = [[Person alloc] initWithFirstName:@"Tom"
lastName:@""
andAge:17];
Person *p5 = [[Person alloc] initWithFirstName:@"Cindy"
lastName:@"Lou"
andAge:6];
Person *p6 = [[Person alloc] initWithFirstName:@"Yanno"
lastName:@"Dirst"
andAge:76];
NSArray *listOfObjects = @[p1,p2,p3,p4,p5,p6];

// 如果打印数组中的每个元素,兑现就会以你将它们放到数组中时的顺序出现,如果想要根据每个人的年龄、姓与名排序数组,那么可以使用NSSortDercriptor对象。需要为用于排序的每个Person属性提供排序描述符:
NSSortDescriptor *sd1 = [NSSortDescriptor     sortDescriptorWithKey:@"age"                                                         ascending:YES];

NSSortDescriptor *sd2 = [NSSortDescriptor sortDescriptorWithKey:@"lastName"                                                      ascending:YES];

NSSortDescriptor *sd3 = [NSSortDescriptor sortDescriptorWithKey:@"firstName"                                                      ascending:YES];

NSArray *sdArray1 = @[sd1,sd2,sd3];

// 需要将属性名以字符串的形式传递进去并指定根据属性进行升序还是降序排列。最后,所有排序描述符需要放在数组中。
// 数组中每个排序描述符的位置决定了对象的排序顺序。因此,如果希望数组先根据年龄,然后根据姓进行排序,那么确保先将年龄对应的排序描述符添加进来,然后再将与姓对应的的排序描述符添加进来。
// 要想获得排好序的数组,请向待排序的数组发送sortedArrayUsingDescriptors:消息并将描述符数组作为参数传递进来:
NSArray *sortedArray1 = [listOfObjects sortedArrayUsingDescriptors:sdArray1];

// 要想查看结果,请使用makeObjectsPerformSelector:方法将排好序的数组中的每个对象的状态输出到日志中:
[sortedArray1 makeObjectsPerformSelector:@selector(reportState)];

NSArray *sdArray2 = @[sd2,sd1,sd3];

NSArray *sortedArray2 = [listOfObjects sortedArrayUsingDescriptors:sdArray2];

[sortedArray2 makeObjectsPerformSelector:@selector(reportState)];


5.查询数组

NSPredicate用于定义搜索查询。接下来,可以使用原始数组的filteredArrayUsingPredicate:函数并根据定义的NSPredicate对象规范返回数组的子集

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age > 30"];

NSArray *arraySubset = [listOfObjects filteredArrayUsingPredicate:predicate];


6.操纵数组内容

NSMutableArray *listOfLetters = [NSMutableArray array];

[listOfLetters addObject:@"A"];
[listOfLetters addObject:@"B"];
[listOfLetters addObject:@"C"];

[listOfLetters insertObject:@"a" atIndex:0];

[listOfLetters replaceObjectAtIndex:2 withObject:@"c"];

[listOfLetters exchangeObjectAtIndex:0 withObjectAtIndex:2];

[listOfLetters removeObject:@"A"];
[listOfLetters removeObjectAtIndex:1];
[listOfLetters removeLastObject];
[listOfLetters removeAllObjects];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: