NSArray、NSMutableArray基本用法
一、基本操作
初始化方法:
1.init返回一个空数组
2.initWithArray从已有数组初始化
3.initWithContentsOfFile//从plist文件加载
4.initWithContentsOfUrl//从网络地址上获取
5.initWithObject用一个对象初始化
6.initWithObjects从多对象初始化
7.self.theDataArray=[[NSMutableArray alloc]initWithCapacity:5];//指定有五个元素初始化
打印第一个元素:
NSLog(@"the object is:%@",[theDataArray firstObjectCommonWithArray:theDataArray]);
打印最后一个元素:
NSLog(@"the object is:%@",[theDataArray lastObject]);
枚举所有元素,方法一:
for (NSString * theStr in theDataArray) {
NSLog(@"%@:",theStr);
}
枚举所有元素,方法二:
for (int i=0,i<[theDataArray count],i++) {
NSLog(@"%@:",[theDataArray objectAtIndex:i]);
}
枚举所有元素,方法三,用枚举器:
NSEnumerator *enumerator=[theDataArray objectEnumerator];
id obj;
while (obj =[enumerator nextObject]) {
NSLog(@"%@",obj);
}
添加元素
[theDataArray addObject:@"这是新添加的元素"];//从最后开始添加
从指定索引插入元素
[theDataArray insertObject:@"this is inerted object" atIndex:0];//是插入到指定 索引的前面
修改.更新元素
[theDataArray replaceObjectAtIndex:0 withObject:@"new obj"];//指定索引修改
判断数组是否包含某个对象
if ([theDataArray containsObject:@"selectiont"]) {
NSLog(@"the object selection is contained in array");
}
else{
NSLog(@"not contain");
}
获取元素索引
NSLog(@"the idx is:%i",[theDataArray indexOfObject:@"selection"]);
数组元素排序
方法一:
NSArray *theTempArr=[[NSArray alloc] initWithArray:[theDataArray
sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]];
[theDataArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
// or @selector(compare:)
方法二:
NSLog(@"before sorted array:%@",theDataArray);
NSCountedSet *cset=[[NSCountedSet alloc] initWithArray:theDataArray];
NSArray *theTempArr=[[NSArray alloc] initWithArray:[[cset allObjects]sortedArrayUsingSelector:@selector(compare:)]];
NSLog(@"after sorted array:%@",theTempArr);
对数组对象进行排序 NSSortDescriptor
NSSortDescriptor *sortDescriptor=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];//@“name”是对象属性
[theDataArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];//返回排序好的数组
//还可以用自定义方法:[theDataArray sortUsingSelector:@selector(custom Method:)];
-(NSInteger)customMethod:(someObject *)otherObj{
NSCompareResult compareResult=[self.name compare:otherObj.name];
if(compareResult == NSOrderedSame) return 0;
if(compareResult == NSOrderedAscending) return 1;
else return -1;
}
对象数组过滤(NSPredicate)
NSArray *keyArray=[[NSArray alloce]initWtihObjects:@"A",@"B",nil];
for (NSString *key in keyArray)
{
//定义规则:
NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"SELF.name
beginswith[c] %@",key]; //SELF.name是对象属性,beginswith[c] %@",key
表示选出name以key开头的对象
//或者 :
NSPredicate *apredicate=[NSPredicate
predicateWithFormat:@"SELF.name contains [c] %@",key]; //contains[c]
%@",key 表示选出name中包含有key的对象
//或者 :
NSInteger age=20;
NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"SELF.age > %i",age]; // 表示选出age>20的对象
//或者 :
NSString * matchStr=@"hel*";
NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"SELF.name like %@",matchStr]; // 表示选出name中包含与字符串matchStr相似的对象
NSArray *newArr=[theDataArray filteredArrayUsingPredicate:apredicate];
[theDataDict setValue:newArr forKey:key];
}
NSMutableArray
删除数组元素
NSMutableArray *tempArray=[[NSMutableArray alloc]initWithObjects:@"one",@"tow",@"threr",nil];
[tempArray removeObjectAtIndex:0];//从指定索引移除
[tempArray removeAllObjects];//移除所有元素
[tempArray removeLastObject];//移除最后那个元素
[tempArray removeObjectsInArray:newArray];//移除指定数组中元素
[tempArray removeObjectsAtIndexes: NSIndexSet *__strong)];//从所选择的索引中移除
(内容均来自于各大IT论坛)