标签:
// ----------------------数组------------------------------------
// 初始化
NSArray *array0 = [NSArray array]; // 空数组
NSArray *array1 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
NSArray *array2 = [[NSArray alloc] initWithArray:array1];
NSArray *array22 = [array1 subarrayWithRange:NSMakeRange(0, 1)];
// 数组取值
NSString *str = [array1 objectAtIndex:1];
NSString *firstStr = [array1 firstObject];
NSString *lastStr = [array1 lastObject];
// 数组索引
int index = [array1 indexOfObject:@"1"];
NSLog(@"index = %i", index);
NSLog(@"str = %@", str);
NSLog(@"array = %@, count = %ld", array2, [array2 count]);
// 数组包含
BOOL containtsObj = [array1 containsObject:@"2"];
// -- 可变数组
NSMutableArray *array3 = [[NSMutableArray alloc] initWithCapacity:1];
[array3 addObjectsFromArray:array2];
[array3 addObject:@"333"];
[array3 removeObjectAtIndex:0];
[array3 setObject:@"000" atIndexedSubscript:0];
NSLog(@"array3 = %@", array3);
// 深拷贝
NSMutableArray *newArray = (NSMutableArray*)CFBridgingRelease(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)array3, kCFPropertyListMutableContainers));
NSLog(@"newArray = %@", newArray);
//数组合并
NSString *str1 = [newArray componentsJoinedByString:@";"];
NSLog(@"str1 = %@", str1);
// 数组排序
// 1.
NSArray *array11 = [NSArray arrayWithObjects:@"1",@"5",@"2",@"6",@"3",@"4",nil];
NSArray *array12 = [array11 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"array12=%@", array12);
//2. 比较英语顺序
NSArray *array13 = [NSArray arrayWithObjects:@"zhangsan", @"lisi", @"wangwu", @"zhangyi", @"lisan", nil];
NSArray *array14 = [array13 sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2)
{
NSComparisonResult result = [str1 compare:str2];
return result;
}];
NSLog(@"array14=%@", array14);
标签:
原文地址:http://www.cnblogs.com/xiangjune/p/4883524.html