/*
NSSet 集合
与NSArray一样:只能存对象,不能直接存基本类型(NSNumber)
与NSArray的区别:NSSet无顺序
*/
//+++++++++++++++++++++++++++++++++++++++++++++
// NSSet *set1=[NSSet set];//空,不用
Car *car1=[Car new];
NSSet *set2=[NSSet setWithObjects:@"a",@"b",@"c",@"d",@"e",car1, nil];//不存重复的元素
NSLog(@"%lu",set2.count);//元素个数
//打印
NSLog(@"%@",set2);
//获取元素
NSArray *ary1=[set2 allObjects];//所有,返回一个有序数组。
NSLog(@"%@",ary1);
[set2 anyObject];//随机取一个☝️
//BOOL 是否包含
[set2 containsObject:@"c"];
if ([set2 containsObject:@"c"]) {
NSLog(@"包含");
}else{
NSLog(@" 不包含");
}
//==================================================
//NSMutableSet
//添加
NSMutableSet *setMut3=[NSMutableSet set];
[setMut3 addObject:@"f"];
[setMut3 addObjectsFromArray:@[@"a",@"b",@"c",@"d",@"e"]];
NSLog(@"%@",setMut3);
//删除
[setMut3 removeObject:@"f"];// 删除一个元素
NSLog(@"%@",setMut3);
[setMut3 removeAllObjects];//删除所有
NSLog(@"%@",setMut3);
//遍历set
NSMutableSet *setMut4=[NSMutableSet setWithObjects:@"a",@"b",@"c",@"d",@"e",@"a",@"b", nil];
for(id obj in setMut4){
NSLog(@"%@",obj);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
NSDictionary 字典
存对象,没有顺序
*/
//======================================
//创建字典
// NSDictionary *dict=[NSDictionary dictionary];//不可变,空的,不用
//存一个数据
NSDictionary *dict1=[NSDictionary dictionaryWithObject:@"jereh" forKey:@"name"];
//取出字典数据
NSLog(@"%@",[dict1 objectForKey:@"name"]);
NSDictionary* dict2=[NSDictionary dictionaryWithObjects:@[@"xiaoHong",@"20",@"001"] forKeys:@[@"name",@"age",@"num"]];
NSLog(@"%@",dict2);
NSDictionary *dict3=[NSDictionary dictionaryWithObjectsAndKeys:@"jereh",@"name",
@"23",@"age" ,nil];//一个值后跟一个key
NSLog(@"%@",dict3);
//常用
NSDictionary *dict4=@{@"name":@"jereh",
@"age":@"20",
@"num":@"001" };//快速创建不可变 字符串,数组,字典
NSLog(@"%@",dict4);
//返回所有数据
NSLog(@"%@",[dict4 allValues]);
//返回key对应的数据
NSLog(@"%@",[dict4 objectForKey:@"name"]);
NSDictionary *dict5=[NSDictionary dictionaryWithDictionary:dict4];
NSLog(@"%@",dict5);
[dict2 objectsForKeys:@[@"name",@"age",@"num"] notFoundMarker:@"No found!"];
NSLog(@"%@",[dict2 objectsForKeys:@[@"name",@"age",@"num",@"sex"] notFoundMarker:@"No found!"]);
//========================================================================
//NSMutableDictionary 可变字典
NSMutableDictionary *dict6=[NSMutableDictionary dictionary];//可变空字典
[dict6 setObject:@"jereh" forKey:@"name1"];
[dict6 addEntriesFromDictionary:dict5];
NSLog(@"%@",dict6);
[dict6 removeObjectForKey:@"name2"];
NSLog(@"%@",dict6);
[dict6 removeObjectsForKeys:@[@"name",@"age",@"name3"]];
NSLog(@"%@",dict6);
for(id objK in dict6){
NSLog(@"key:%@ value:%@",objK,[dict6 objectForKey:objK]);
}//便利输出字典
NSSet 集合
与NSArray一样:只能存对象,不能直接存基本类型(NSNumber)
与NSArray的区别:NSSet无顺序
*/
//+++++++++++++++++++++++++++++++++++++++++++++
// NSSet *set1=[NSSet set];//空,不用
Car *car1=[Car new];
NSSet *set2=[NSSet setWithObjects:@"a",@"b",@"c",@"d",@"e",car1, nil];//不存重复的元素
NSLog(@"%lu",set2.count);//元素个数
//打印
NSLog(@"%@",set2);
//获取元素
NSArray *ary1=[set2 allObjects];//所有,返回一个有序数组。
NSLog(@"%@",ary1);
[set2 anyObject];//随机取一个☝️
//BOOL 是否包含
[set2 containsObject:@"c"];
if ([set2 containsObject:@"c"]) {
NSLog(@"包含");
}else{
NSLog(@" 不包含");
}
//==================================================
//NSMutableSet
//添加
NSMutableSet *setMut3=[NSMutableSet set];
[setMut3 addObject:@"f"];
[setMut3 addObjectsFromArray:@[@"a",@"b",@"c",@"d",@"e"]];
NSLog(@"%@",setMut3);
//删除
[setMut3 removeObject:@"f"];// 删除一个元素
NSLog(@"%@",setMut3);
[setMut3 removeAllObjects];//删除所有
NSLog(@"%@",setMut3);
//遍历set
NSMutableSet *setMut4=[NSMutableSet setWithObjects:@"a",@"b",@"c",@"d",@"e",@"a",@"b", nil];
for(id obj in setMut4){
NSLog(@"%@",obj);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
NSDictionary 字典
存对象,没有顺序
*/
//======================================
//创建字典
// NSDictionary *dict=[NSDictionary dictionary];//不可变,空的,不用
//存一个数据
NSDictionary *dict1=[NSDictionary dictionaryWithObject:@"jereh" forKey:@"name"];
//取出字典数据
NSLog(@"%@",[dict1 objectForKey:@"name"]);
NSDictionary* dict2=[NSDictionary dictionaryWithObjects:@[@"xiaoHong",@"20",@"001"] forKeys:@[@"name",@"age",@"num"]];
NSLog(@"%@",dict2);
NSDictionary *dict3=[NSDictionary dictionaryWithObjectsAndKeys:@"jereh",@"name",
@"23",@"age" ,nil];//一个值后跟一个key
NSLog(@"%@",dict3);
//常用
NSDictionary *dict4=@{@"name":@"jereh",
@"age":@"20",
@"num":@"001" };//快速创建不可变 字符串,数组,字典
NSLog(@"%@",dict4);
//返回所有数据
NSLog(@"%@",[dict4 allValues]);
//返回key对应的数据
NSLog(@"%@",[dict4 objectForKey:@"name"]);
NSDictionary *dict5=[NSDictionary dictionaryWithDictionary:dict4];
NSLog(@"%@",dict5);
[dict2 objectsForKeys:@[@"name",@"age",@"num"] notFoundMarker:@"No found!"];
NSLog(@"%@",[dict2 objectsForKeys:@[@"name",@"age",@"num",@"sex"] notFoundMarker:@"No found!"]);
//========================================================================
//NSMutableDictionary 可变字典
NSMutableDictionary *dict6=[NSMutableDictionary dictionary];//可变空字典
[dict6 setObject:@"jereh" forKey:@"name1"];
[dict6 addEntriesFromDictionary:dict5];
NSLog(@"%@",dict6);
[dict6 removeObjectForKey:@"name2"];
NSLog(@"%@",dict6);
[dict6 removeObjectsForKeys:@[@"name",@"age",@"name3"]];
NSLog(@"%@",dict6);
for(id objK in dict6){
NSLog(@"key:%@ value:%@",objK,[dict6 objectForKey:objK]);
}//便利输出字典