NSArray和NSMutableArray的常用方法
/* 初始化方法:
1.init返回一个空数组
2.initWithArray从已有数组初始化
3.initWithContentsOfFile//从plist文件加载
4.initWithContentsOfUrl//从网络地址上获取
5.initWithObject用一个对象初始化
6.initWithObjects从多对象初始化
self.theDataArray=[[NSMutableArray alloc]initWithCapacity:5];//指定有五个元素初始化
*/
/*打印第一个元素:
NSLog(@"the object is:%@",[theDataArray firstObjectCommonWithArray:theDataArray]);
*/
/*打印最后一个元素:
NSLog(@"the object is:%@",[theDataArray lastObject]);
//枚举所有元素,方法一:
NSArray *oldArray = [[NSArray alloc] initWithObjects:@"c",@"a",@"b",@"A",@"C", nil];
for (NSString * obj in oldArray) {
NSLog(@"%@:",obj);
}
//枚举所有元素,方法二:
for (int i=0;i<[oldArray count];i++) {
NSLog(@"%@:",[oldArray objectAtIndex:i]);
}
// 枚举所有元素,方法三,用枚举器:
NSEnumerator *enumerator=[oldArray 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 *oldArray = [[NSArray alloc] initWithObjects:@"c",@"a",@"b",@"A",@"C", nil];
NSArray *newArray = [oldArray sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"newArray===%@",newArray);
//方法二:(借用NSSet)
//@selector(compare:)区分大小写@selector(caseInsensitiveCompare:)不区分大小写
NSArray *oldArray = [[NSArray alloc] initWithObjects:@"c",@"a",@"b",@"A",@"C",nil];
NSSet *aSet=[[NSSet alloc] initWithArray:oldArray];
NSArray *newArray=[[NSArray alloc] initWithArray:[[aSet allObjects]sortedArrayUsingSelector:@selector(compare:)]];
NSLog(@"newArray==%@",newArray);
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 *tempArray=[[NSMutableArray alloc]initWithObjects:@"one",@"tow",@"threr",nil];
[tempArray removeObjectAtIndex:0];//从指定索引移除
[tempArray removeAllObjects];//移除所有元素
[tempArray removeLastObject];//移除最后那个元素
[tempArray removeObjectsInArray:newArray];//移除指定数组中元素
[tempArray removeObjectsAtIndexes: NSIndexSet *__strong)];//从所选择的索引中移除
*/