oc中的集合类
分类:程序语言|标签:IOS|日期: 2015-03-07 02:00:15
OC中的集合有:NSArray 数组 NSDictionary 字典 NSSet 集合
第一:NSArrary
1.1:集合的基本方法
NSArray * array = [[NSArray alloc] init];
NSArray * array1 = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
NSArray * array2 = [[NSArray alloc] initWithObjects:@"one",@"two",number, nil];
NSArray * karray = @[@"a",@"b",@"c"];
NSString * kstr = karray[0];
NSString * str = [array objectAtIndex:0];
NSLog(@"str %@",str);
NSUInteger arrayCount = [array count];
BOOL isContain = [array containsObject:id];
特性:
1.数组中可以存储不同类型的对象
2.数组实际上存储的时对象的地址,同样也可以存储数组的地址
3.存储自定义的对象 (最好都给我重写 description方法)
4.数组中存储基本数据类型, (如果你要在数组中存储基本数据类型,请包装好了之后再去存 NSNumber)
5.注意不要把nil值存储到NSArray中,会导致数据丢失
1.2:数组排序:
1.使用sortedArrayUsingSelector
NSArray * array = @[@"b",@"d",@"a",@"z"];
array = [array sortedArrayUsingSelector:@selector(compare:)];
2.使用block方式排序
array2 = [array2 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj2 compare:obj1];
}];
3.使用自定义对象排序 :如果你向给你自己定义的对象排序,必须根据某一个属性来排序,
NSSortDescriptor * d1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
NSSortDescriptor * d2 = [NSSortDescriptor sortDescriptorWithKey:@"year" ascending:NO];
NSArray * descripts = @[d2,d1];
array3 = [array3 sortedArrayUsingDescriptors:descripts];
1.3:可变数组:
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:0];
[array addObject:str1];
[array insertObject:str1 atIndex:2];
[array removeObject:str1];
[array removeObjectAtIndex:0];
[array addObject:str2];
[array addObject:str3];
[array addObject:str1];
[array removeAllObjects];
第二:NSDictionary
第一:NSDictionary
字典是以键值对的形式来存储数据 key value
字典的顺序不是按照存储时候的顺序。
字典中可以存任意数据类型
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"apple",@"name", nil];
NSDictionary * dic2 = @{ @"one":@"1",@"two":@"2"};
dic.count
NSString * str = [dic3 objectForKey:@"one"];
NSArray * allkeys = [dic3 allKeys];
for (int i = 0; i < allkeys.count; i++)
{
NSString * key = [allkeys objectAtIndex:i];
id obj = [dic3 objectForKey:key];
}
NSEnumerator * enumerator = [dic3 objectEnumerator];
id value;
while (value = [enumerator nextObject]) {
NSLog(@"value %@",value);
}
第三:NSSet
NSString * str1 = @"one";
NSString * str2 = @"two";
NSString * str3 = @"three";
NSSet * set = [[NSSet alloc] initWithObjects:str1,str2,str3,str1, nil];
NSEnumerator * enumerator = [set objectEnumerator];
NSString * value;
while (value = [enumerator nextObject]) {
NSLog(@"value %@",value);
}
NSMutableSet * muSet = [[NSMutableSet alloc] init];
[muSet addObject:str1];
[muSet removeObject:str1];
[muSet removeAllObjects];
NSEnumerator * en = [muSet objectEnumerator];
NSString * value;
while (value = [en nextObject]) {
NSLog(@"value %@",value);
}
综合:集合相互转换
NSArray * array = @[@"one",@"two",@"three"];
NSMutableArray * muArray = [NSMutableArray arrayWithArray:array];
NSLog(@"muarray %@",muArray);
NSDictionary * dic = @{@"one":@"1",@"two":@"2"};
NSMutableDictionary * muDic = [NSMutableDictionary dictionaryWithDictionary:dic];
NSLog(@"mudic %@ ",muDic);
NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two", nil];
NSMutableSet *muSet = [NSMutableSet setWithSet:set];
NSLog(@"muSet %@",muSet);
NSMutableSet * muSet2 = [NSMutableSet setWithArray:array];
NSLog(@"muSet2 %@",muSet2);
NSArray * allkeys = [dic allKeys];
NSLog(@"allkeys %@",allkeys);
NSArray * allValues = [dic allValues];
NSLog(@"allValues %@",allValues);
NSString * str = @"www.itacast.cn";
NSArray * strArray = [str componentsSeparatedByString:@"."];
NSLog(@"strArray %@",strArray);