集合
2019-02-19 本文已影响0人
习惯了_就好
/*
NSSet是无序的
NSSet中不能存储重复的数据,可以o用来去除重复的值
*/
NSSet * set = [[NSSet alloc]initWithObjects:@"a",@"b",@"c", nil];
NSLog(@"set中的值为%@",set);
//NSSet的长度
NSLog(@"set的长度是:%ld",set.count);
//NSSet是否包含某个值
if([set containsObject:@"b"]){
NSLog(@"包含b");
}else{
NSLog(@"不包含b");
}
//遍历
NSArray * array = [set allObjects];
for (int i = 0; i<array.count; i++) {
NSLog(@"value = %@",array[i] );
}
NSEnumerator * enumerator = [set objectEnumerator];
id value;
while (value = [enumerator nextObject]) {
NSLog(@"value = %@",value);
}
//可变集合
//创建一个可变集合
NSMutableSet * mutableSet = [[NSMutableSet alloc]initWithCapacity:0];
//添加数据
[mutableSet addObject:@"one"];
[mutableSet addObject:@"two"];
NSLog(@"mutableSet值为:%@",mutableSet);
//遍历数据
NSEnumerator * enumerator2 = [mutableSet objectEnumerator];
NSString * value2;
while (value2 = [enumerator2 nextObject]) {
NSLog(@"mutableSet值分别为:%@",value2);
}
//删除数据
[mutableSet removeObject:@"one"];
NSLog(@"mutableSet值为:%@",mutableSet);
//删除所有元素
[mutableSet removeAllObjects];
NSLog(@"mutableSet值为:%@",mutableSet);
/*
集合之间的转换
*/
//NSArray -> NSMutableArray
NSArray * nsNrray = @[@"a",@"b",@"c"];
NSMutableArray * nsMutableArray = [NSMutableArray arrayWithArray:nsNrray];
NSLog(@"NSArray -> NSMutableArray : %@",nsMutableArray);
//NSDictionary -> NSMutableDictionary
NSDictionary * dictionary = @{@"one":[NSNumber numberWithInt:1],@"two":[NSNumber numberWithInt:2]};
NSMutableDictionary * mutableDic = [NSMutableDictionary dictionaryWithDictionary:dictionary];
NSLog(@"NSDictionary -> NSMutableDictionary : %@",mutableDic);
//NSSet -> NSMutableSet
NSSet * nsSet = [[NSSet alloc]initWithObjects:@"a",@"b", nil];
NSMutableSet * nsMutableSet = [NSMutableSet setWithSet:nsSet];
NSLog(@"NSSet -> NSMutableSet : %@",nsMutableSet);
//NSArray -> NSSet
NSLog(@"NSArray -> NSSet %@",[NSSet setWithArray:nsNrray]);
//NSDictionary -> NSArray
NSLog(@"NSDictionary ->key的 NSArray %@",[dictionary allKeys]);
NSLog(@"NSDictionary ->value的 NSArray %@",[dictionary allValues]);
//NSString -> NSArray
NSString * str = @"www.baidu.com";
NSLog(@"NSString -> NSArray : %@",[str componentsSeparatedByString:@"."]);