首页 > 代码库 > Object-C—集合
Object-C—集合
Obejct-C中包含了三种集合,分别是:数组、字典和集(set)。
- 数组
NSLog(@"string:%@",string);
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
[string release];
//从数组合并元素到字符串- componentsJoinedByString:
NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString *string = [array componentsJoinedByString:@","];
NSLog(@"string:%@",string);
删除可变数组指定索引处的对象:
[Mutablearray removeObjectAtIndex:1];
- 创建字典
NSString *string = [dictionary objectForKey:@"One"];
NSLog(@"string:%@",string);
NSLog(@"dictionary:%@",dictionary);
[dictionary release];
创建可变字典:
//创建
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
//添加字典
[dictionary setObject:@"One" forKey:@"1"];
[dictionary setObject:@"Two" forKey:@"2"];
[dictionary setObject:@"Three" forKey:@"3"];
[dictionary setObject:@"Four" forKey:@"4"];
NSLog(@"dictionary:%@",dictionary);
//删除指定的字典
[dictionary removeObjectForKey:@"3"];
NSLog(@"dictionary:%@",dictionary);
- 集合NSSet
//NSSet中不能存在重复的对象
NSMutableSet *set1 = [[NSMutableSet alloc] initWithObjects:@"1",@"2",@"3", nil];
NSMutableSet *set2 = [[NSMutableSet alloc] initWithObjects:@"1",@"5",@"6", nil];
[set1 unionSet:set2]; //取并集1,2,3,5,6
[set1 intersectSet:set2]; //取交集1
[set1 minusSet:set2]; //取差集2,3,5,6
相关文章:
http://wenku.baidu.com/link?url=3wlXip1sYdUBWfXjFi58bc_MJChNoMAgA00ooevx8RQCForbdQMxHwJ43DQAAMfL_O7vgR0r1034DWLVWRGCpNjUbigqi1yM-RLEYO2R7fS