- NSArray基本用法
void arrayTest1() { //数组初始化最后必须以nil结尾,表示数组元素结束 NSArray *array1 = [[NSArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",@"item4",nil]; NSLog(@"%@",array1); /*( item1, item2, item3, item4 )*/ //获取数组元素个数 NSLog(@"array count : %ld",array1.count); /*array count : 4*/ //获取特定所以元素 NSLog(@"array[1] = %@",array1[1]); /* array[1] = item1 */ NSLog(@"array[1] = %@",[array1 objectAtIndex:(NSInteger)1]); /* array[1] = item1 */ NSLog(@"firstObject = %@",array1.firstObject); //获取第一个元素 /* firstObject = item1 */ NSLog(@"lastObject = %@",array1.lastObject); //获取最后一个元素 /* lastObject = item4 */ //拼接数组 NSLog(@"%@",[array1 componentsJoinedByString:@","]); /* item0,item1,item2,item3,item4 */ //查找元素的索引,如果包含特定元素,返回具体索引,否则返回:9223372036854775807 NSLog(@"%ld",[array1 indexOfObject:@"item11"]); //是否包含特定元素 NSLog(@"%i", [array1 containsObject:@"item1"] == TRUE ? 1 : 0); /* 1 */ NSArray *subArray = [array1 subarrayWithRange:NSMakeRange(1, 2)]; NSLog(@"%@",subArray); /* ( item1, item2 ) */ //保存数组元素到本地文件,以xml格式序列化存储 [array1 writeToFile:@"/Users/new/Desktop/array.txt" atomically:YES]; /* <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <string>item0</string> <string>item1</string> <string>item2</string> <string>item3</string> <string>item4</string> </array> </plist> */ //读取本地文件,反序列化为数组 NSArray *array2 = [[NSArray alloc]initWithContentsOfFile:@"/Users/new/Desktop/array.txt"]; NSLog(@"%@",array2); /* ( item0, item1, item2, item3, item4 ) */ //判断数组元素是否相等 BOOL result = [array1 isEqualToArray:@[@"item0",@"item1",@"item2"]]; NSLog(@"%@",result); /* 0 */
}
- NSArray遍历
void arrayTest2()
{
NSArray *array1 = [[NSArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",@"item4",nil];
//遍历方法1
for (int index = 0; index < array1.count; index++) {
NSLog(@"%@",array1[index]);
}
/*
item0
item1
item2
item3
item4
*/
//遍历方法2
for (NSString *item in array1) {
NSLog(@"%@",item);
}
/*
item0
item1
item2
item3
item4
*/
//遍历方法3
//NSEnumerator *enumerator = [array1 reverseObjectEnumerator]; //逆向遍历
NSEnumerator *enumerator = [array1 objectEnumerator]; //正向遍历
NSString *item = nil;
while (item = [enumerator nextObject]) {
NSLog(@"%@",item);
}
/*
item0
item1
item2
item3
item4
*/
//遍历方法4
[array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%ld = %@",idx,obj);
}];
/*
0 = item0
1 = item1
2 = item2
3 = item3
4 = item4
*/
}
- NSArray排序
void arrayTest3()
{
//排序方法1,普通比较器
NSArray *array1 = @[@"1",@"21",@"11",@"5",@"51",@"3"];
NSArray *array2 = [array1 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",array2);
/*
(
1,
11,
21,
3,
5,
51
)
*/
//排序方法2,代码块
NSArray *array3 = [array1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
NSLog(@"%@",array3);
/*
(
1,
11,
21,
3,
5,
51
)
*/
//排序方法3,自定义排序描述符
NSArray *originalArray = @[
@{@"page_no":@"27",@"age":@24},
@{@"page_no":@"1", @"age":@23},
@{@"page_no":@"1", @"age":@21},
@{@"page_no":@"1", @"age":@25},
@{@"page_no":@"1", @"age":@15},
@{@"page_no":@"12",@"age":@19},
@{@"page_no":@"23",@"age":@29},
@{@"page_no":@"3", @"age":@22},
@{@"page_no":@"2", @"age":@30},
@{@"page_no":@"17",@"age":@33}
];
NSSortDescriptor *alphaNumSD = [NSSortDescriptor sortDescriptorWithKey:@"page_no" ascending:YES comparator:^NSComparisonResult(NSString *string1, NSString *string2) {
return [string1 compare:string2 options:NSNumericSearch];
}];
NSSortDescriptor *dataNumSD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES comparator:^NSComparisonResult(id data1, id data2) {
return [data1 compare:data2];
}];
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:@[alphaNumSD,dataNumSD]];
NSLog(@"%@",sortedArray);
/*
(
{
age = 15;
"page_no" = 1;
},
{
age = 21;
"page_no" = 1;
},
{
age = 23;
"page_no" = 1;
},
{
age = 25;
"page_no" = 1;
},
{
age = 30;
"page_no" = 2;
},
{
age = 22;
"page_no" = 3;
},
{
age = 19;
"page_no" = 12;
},
{
age = 33;
"page_no" = 17;
},
{
age = 29;
"page_no" = 23;
},
{
age = 24;
"page_no" = 27;
}
)
*/
//排序方法4,自定义比较方法
Person *person1 = [[Person alloc]initWithFirstName:@"hello" lastName:@"world" age:35];
Person *person2 = [[Person alloc]initWithFirstName:@"abc" lastName:@"def" age:25];
Person *person3 = [[Person alloc]initWithFirstName:@"cal" lastName:@"kdl" age:22];
NSArray *personArray = @[person1,person2,person3];
NSArray *tempArray = [personArray sortedArrayUsingSelector:@selector(comparePeople:)];
NSLog(@"%@",tempArray);
/*
(
" firstName: abc,lastName:def,age:25",
" firstName: cal,lastName:kdl,age:22",
" firstName: hello,lastName:world,age:35"
)
*/
}
-
NSMutableArray
NSMutableArray继承NSArray,所以NSArray的所有特性NSMutableArray都有,在此基础上,NSMutableArray主要新增了添加、删除、更新、插入等特性,如下代码演示:
void arrrayTest4()
{
NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",@"item4", nil];
NSLog(@"%@",mutableArray);
/*
(
item0,
item1,
item2,
item3,
item4
)
*/
//添加元素
[mutableArray addObject:@"item5"];
NSLog(@"%@",mutableArray);
/*
(
item0,
item1,
item2,
item3,
item4,
item5
)
*/
//插入元素
[mutableArray insertObject:@"inserted item" atIndex:(NSInteger)1];
NSLog(@"%@",mutableArray);
/*
(
item0,
"inserted item",
item1,
item2,
item3,
item4,
item5
)
*/
//删除元素
[mutableArray removeObject:@"item5"];
NSLog(@"%@",mutableArray);
/*
(
item0,
item1,
item2,
item3,
item4
)
*/
//更新元素
[mutableArray replaceObjectAtIndex:(NSInteger)5 withObject:@"replacedItem"];
NSLog(@"%@",mutableArray);
/*
(
item0,
"inserted item",
item1,
item2,
item3,
replacedItem
)
*/
//删除所有元素
[mutableArray removeAllObjects];
NSLog(@"%ld",mutableArray.count);
/* 0 */
}

浙公网安备 33010602011771号