概述
原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
前言:本文会首先讲一下本人使用NSArray的几个小技巧,然后详解下常用的属性和方法。
一 NSArray/NSMutableArray简介
NSArray存储的是一组对象的数组,这些对象是有序的,NSArray内容不可改变,如果需要可改变的使用NSMutableArray,它是NSArray的子类,在Cocoa touch中处于Core Service层。当然,也可以继承NSArray来自定义自己的数组,不过这种情况极少,这里不做讲解。通常如果需要Array的其他,创建类别(category足矣)
继承关系:NSArray->NSObject
遵循协议: NSCopying,NSFastEnumeration,NSObject,NSMutableCopying,NSSecureCoding
NSMutableArray继承自NSArray。
继承关系:NSMutableArray->NSArray->NSObject
遵循协议:NSCopying,NSFastEnumeration,NSObject,NSMutableCopying,NSSecureCoding
二 使用NSArray的小技巧
2.1 快捷创建符号@[]
例如
NSArray *array = @[@“1",@"2",@"3"];
2.2 firstObject:安全返回第一个元素
取NSArray有两种方式,用array[0]在数组为空的时候会报错,用[array firstObject]即使数组为空,也不会报错,会返回nil
同理lastObject也一样,
2.3 makeObjectsPerformSelector:withObject: 和makeObjectsPerformSelector:让每个数组内元素都执行某个SEL,这样写就不必再写个for语句了
2.4 KVC的方式取值,做计算
例如有个数组:
NSArray * array = @[
@{@"name":@"hwc",
@"count":@(10),
@"url":@"blog.csdn.net/hello_hwc"
},
@{@"name":@"baidu",
@"count":@(20),
@"url":@"www.baidu.com"
},
@{@"name":@"google",
@"count":@(22),
@"url":@"www.google.com"
}
];
NSArray * nameArray = [array valueForKeyPath:@"name"];
NSNumber *sum = [array valueForKeyPath:@"@sum.count"];
NSNumber *max = [array valueForKeyPath:@"@max.count"];
NSNumber *min = [array valueForKeyPath:@"@min.count"];
NSLog(@"NameArray:%@",nameArray.description);
NSLog(@"Sum:%@",sum.description);
NSLog(@"max:%@",max.description);
NSLog(@"min:%@",min.description);
输出
HwcFoundationExample[1048:42991] NameArray:(
hwc,
baidu,
google
)
2015-01-12 14:10:45.357 HwcFoundationExample[1048:42991] Sum:52
2015-01-12 14:10:45.357 HwcFoundationExample[1048:42991] max:22
2015-01-12 14:10:45.357 HwcFoundationExample[1048:42991] min:10
三 NSArray常用属性方法详解
为了更加直观,通过一个例子来展示,常用的使用属性和方法几乎都可以从例子中找到。
NSArray例子
//初始化(Initializing an array)
NSArray * array = [[NSArray alloc] initWithObjects:@"first",@"thrid",@"Second", nil];
//查找(Querying an array)
NSString * toFindString = @"Second";
if ([array containsObject:toFindString]) {
NSUInteger index = [array indexOfObject:toFindString];
NSLog(@"%@ index is %lu",toFindString,index);
}
NSString * firstObject = [array firstObject];
NSString * lastObject = [array lastObject];
if (firstObject!=nil) {
NSLog(@"First object is:%@",firstObject);
}
if (lastObject!=nil) {
NSLog(@"LastObject object is:%@",lastObject);
}
//排序(Sort)
NSArray * anotherArray = @[@"1",@"4",@"3"];
NSArray * sortedArrayWithSEL = [array sortedArrayUsingSelector:@selector(localizedCompare:)];
NSArray * sortedArrayWithComparator = [anotherArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString * str1 = obj1;
NSString * str2 = obj2;
if (str1.integerValue > str2.integerValue) {
return NSOrderedDescending;
}
if (str1.integerValue < str2.integerValue) {
return NSOrderedAscending;
}
return NSOrderedSame;
}];
if ([sortedArrayWithComparator isEqualToArray:sortedArrayWithSEL]) {
NSLog(@"The array is same");
}
else{
NSLog(@"The array is not same");
}
//与文件进行操作(Working with file)
//如果存在则读到数组里,如果不存在则写到文件里(If exist read,else write to file)
NSFileManager * defaultManager = [NSFileManager defaultManager];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentDirectory = [paths firstObject];
NSString * fileName = @"TestArray.plist";
NSString * filePath = [documentDirectory stringByAppendingPathComponent:fileName];
BOOL fileExist = [defaultManager fileExistsAtPath:filePath];
if (fileExist) {
//从文件读取
NSArray * readArray = [NSArray arrayWithContentsOfFile:filePath];
//遍历
[readArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSString class]]) {
NSString * str = obj;
NSLog(@"At index:%lu is %@",idx,str);
if ([str isEqualToString:@"thrid"]) {
*stop = YES;
}
}
}];
}else{
[anotherArray writeToFile:filePath atomically:YES];
}
NSMutableArray
//(初始化)Init
NSMutableArray* mutableArray = [[NSMutableArray alloc] initWithObjects:@"2",@"3",@"4", nil];
//(添加对象)Add object
[mutableArray addObject:@"5"];
[mutableArray insertObject:@"1" atIndex:0];
//(删除对象)Remove object
[mutableArray removeObject:@"3"];
[mutableArray removeObjectAtIndex:0];
[mutableArray removeLastObject];
这段代码第一次运算输出
2015-01-14 19:48:46.499 HwcNSArrayExample[509:7777] Second index is 2
2015-01-14 19:48:46.501 HwcNSArrayExample[509:7777] First object is:first
2015-01-14 19:48:46.501 HwcNSArrayExample[509:7777] LastObject object is:Second
2015-01-14 19:48:46.502 HwcNSArrayExample[509:7777] The array is not same
第二次运算输出
2015-01-14 19:49:31.135 HwcNSArrayExample[525:8151] Second index is 2
2015-01-14 19:49:31.135 HwcNSArrayExample[525:8151] First object is:first
2015-01-14 19:49:31.136 HwcNSArrayExample[525:8151] LastObject object is:Second
2015-01-14 19:49:31.136 HwcNSArrayExample[525:8151] The array is not same
2015-01-14 19:49:31.136 HwcNSArrayExample[525:8151] At index:0 is 1
2015-01-14 19:49:31.137 HwcNSArrayExample[525:8151] At index:1 is 4
2015-01-14 19:49:31.137 HwcNSArrayExample[525:8151] At index:2 is 3
注意,在使用writeToFile的时候,Array中的对象类型可以是
NSString,NSData,NSDate,NSNumber,NSArray,NSDictionary
不可以是其他类型的对象
至于完整的属性和方法,见官方文档,还是那句话,一定要能看懂官方文档。
最后
以上就是辛勤水池为你收集整理的IOS SDK详解之NSArray/NSMutableArray的全部内容,希望文章能够帮你解决IOS SDK详解之NSArray/NSMutableArray所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复