OC学习之NSDictionary,NSSet

  1. #import  

  2. int main(int argc, const char argv[])  
  3.  
  4.   
  5.     @autoreleasepool  
  6. //        NSInteger int1 123;  
  7. //        NSNumber *number1 [NSNumber numberWithInteger:int1];  
  8. //        NSLog(@"number1:%@",number1);  
  9. //          
  10. //        NSInteger int2 [number1 integerValue];  
  11. //        NSLog(@"int2:%ld",int2);  
  12. //          
  13. //        float pi 3.1415;  
  14. //        NSNumber *number2 [NSNumber numberWithFloat:pi];  
  15. //          
  16. //        float qq [number2 floatValue];  
  17. //        NSLog(@"QQ:%lf",qq);  
  18. //  
  19. #if  
  20.         //创建字典  
  21.           
  22.         NSDictionary *dictionary1 [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];  
  23.           
  24.           
  25.         NSDictionary *dictionary2 [NSDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2",@"v3",@"k3",@"v4",@"k4",@"v5",@"k5", nil]; //创建一对【键对值】  
  26.           
  27.         NSDictionary *dictionary3 [NSDictionary dictionaryWithDictionary:dictionary1];  
  28.           
  29.           
  30.         NSLog(@"dictionary1:%@",dictionary1);//key value;  
  31.         NSLog(@"dictionary2:%@",dictionary2);  
  32.           
  33.         NSLog(@"dictionary3:%@",dictionary3);//key value;  
  34.           
  35.        
  36.         //将数组转换为字典  
  37.         NSArray *array1 [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G", nil];  
  38.         NSArray *array2 [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g", nil];  
  39.         NSDictionary *dictionary4 [NSDictionary dictionaryWithObjects:array1 forKeys:array2];  
  40.         NSLog(@"dictionary4:%@",dictionary4);  
  41.           
  42.         //获取字典中的元素个数  
  43.         NSUInteger count [dictionary4 count];  
  44.         NSLog(@"dictionary4:%ld",count);//dictionary4:7  
  45.           
  46.         //根据key值找出索引的内容  
  47.         NSString *string1 [dictionary4 objectForKey:@"e"];  
  48.         NSLog(@"the key of dictionary4:%@",string1);//the key of dictionary4:E  
  49.           
  50.         NSArray *array3 [dictionary2 allKeys];//获取字典中所有的key值  
  51.           
  52.       
  53.         NSArray *array4 [dictionary2 allValues];//获取字典种所有value  
  54.         NSLog(@"array3:%@\narray4:%@",array3,array4);  
  55.           
  56.         NSLog(@"************************************************************");  
  57.         //转换成枚举,一般用于循环遍历  
  58.         NSEnumerator *enum1 [dictionary2 keyEnumerator];  
  59.         for (NSString *string2 in enum1)  
  60.             NSLog(@"%@",string2);  
  61.          
  62.           
  63.         //创建一个空的可变字典  
  64. //        NSMutableDictionary *mutableDic1 [[NSMusicDirectory alloc]init];  
  65. //        NSMutableDictionary *mutableDic2 [NSMutableDictionary dictionary];  
  66.           
  67.         NSMutableDictionary *mutableDic1 [NSMutableDictionary dictionaryWithObject:@"A" forKey:@"a"];  
  68.         NSLog(@"添加一个键值对mutableDic1:%@",mutableDic1);//添加一个键值对  
  69.           
  70.         [mutableDic1 setValue:@"b" forKey:@"b"];  
  71.         NSLog(@"%@",mutableDic1);  
  72.           
  73.         NSMutableDictionary *mutableDic2 [NSMutableDictionary dictionaryWithDictionary:dictionary1];  
  74.         [mutableDic2 setDictionary:dictionary1];//可以通过setDictionary方法对一个空的可变字典赋值  
  75.         NSLog(@"对一个空的可变字典赋值%@",mutableDic2);  
  76.           
  77.         [mutableDic1 addEntriesFromDictionary:dictionary1];  
  78.                NSLog(@"添加一个字典到可变%@",mutableDic1);  
  79.           
  80.         [mutableDic1 removeObjectForKey:@"b"];  
  81.         NSLog(@"删除后的可变字典%@",mutableDic1);  
  82.           
  83. #endif  
  84.         NSSet *set1 [NSSet setWithObjects:@"a",@"b",@"c",@"d", nil];  
  85.         NSLog(@"set1:%@",set1);  
  86.           
  87.         NSSet *set2 [[NSSet alloc]initWithObjects:@"x",@"y",@"z", nil ];  
  88.         NSLog(@"set2:%@",set2);  
  89.           
  90.         NSSet *set3 [NSSet setWithSet:set1];  
  91.         NSLog(@"set3:%@",set3);  
  92.           
  93.         NSArray *array =[NSArray arrayWithObjects:@"x",@"y",@"z",@"o",nil];  
  94.         NSSet *set4 [NSSet setWithArray:array];  
  95.         NSLog(@"%@",set4);  
  96.           
  97.         NSLog(@"获取set4中所有元素:%@",[set4 allObjects]);  
  98.         NSLog(@"获取set4中某一个元素:%@",[set4 anyObject]);  
  99.           
  100.         NSSet *set5 [set1 setByAddingObject:@"p"];  
  101.         NSSet *set6 [set1 setByAddingObjectsFromArray:array];  
  102.         NSSet* set7 [set1 setByAddingObjectsFromSet:set2];  
  103.           
  104.         NSLog(@"将set1里面的所有元素放到set5里面但是没有改变set1里面的元素,set5在初始化的时候就添加了一个元素“p”%@",set5);  
  105.         NSLog(@"将数组里面的所有元素放在集合种,但是会把重复的元素去掉%@",set6);  
  106.         NSLog(@"根据已有的集合创建集合复制集合产生新的集合%@",set7);  
  107.           
  108.           
  109.         BOOL isCon [set1 containsObject:@"a"];  
  110.         NSLog(@"判断某元素是不是在集合中存在:%d",isCon);  
  111.         if (isCon == 1)  
  112.             NSLog(@"found");  
  113.          
  114.         else  
  115.          
  116.             NSLog(@"not found");  
  117.           
  118.           
  119.         NSSet *set8 [NSSet setWithObjects:@"a",@"b",@"c",@"1", nil];  
  120.         NSSet *set9 [NSSet setWithObjects:@"a",@"d",@"e", nil];  
  121.         NSSet *set10 [NSSet setWithObjects:@"x",@"y",@"z", nil];  
  122.         NSSet *set11 [NSSet setWithObjects:@"x",@"y", nil];  
  123.         BOOL isEqual [set8 isEqualToSet:set9];//判断两个集合是否匹配  
  124.         if (isEqual == 1)  
  125.             NSLog(@"equal");  
  126.          
  127.         else{  
  128.             NSLog(@"not equal");  
  129.          
  130.         BOOL isInset [set8 intersectsSet:set10];//判断两个集合是否存在交集  
  131.         if (isInset == 1)  
  132.             NSLog(@"yes");  
  133.          
  134.         else  
  135.          
  136.         NSLog(@"no");  
  137.          
  138.         BOOL isSub [set11 isSubsetOfSet:set10];//判断两个集合是否是另一个的字迹  
  139.         if (isSub == 1)  
  140.             NSLog(@"set11 是 set10 的子集");  
  141.          
  142.         else  
  143.          
  144.             NSLog(@"set11 不是 set10 的子集");  
  145.          
  146.   
  147.      NSLog(@"************************************************************");  
  148.         NSMutableSet *mutableSet1 [NSMutableSet setWithObjects:@"1",@"2",@"3",@"4", nil];  
  149. //       NSLog(@"%@",mutableSet1);  
  150.         NSMutableSet *mutableSet2 [NSMutableSet setWithObjects:@"3",@"4",@"5", nil];  
  151.           
  152. //        [mutableSet2 minusSet:mutableSet1];//两个集合相减,即当前者和后者有交集的时候,减去前者的交集,后者不变  
  153. //        NSLog(@"两个集合相减%@",mutableSet2);  
  154.       
  155.         [mutableSet1 unionSet:mutableSet2];//两个集合相加,即当前者和后者有交集的时候,前者加上后者多出的元素,后者不变  
  156.         NSLog(@"两个集合相加:%@",mutableSet1);  
  157.           
  158.     
  159.         [mutableSet1 intersectSet:set8];//获取不可变的集合与可变集合的交集  
  160.         NSLog(@"获得两个集合的交集%@",mutableSet1);  
  161.       
  162.      
  163.     return 0;  
  164.  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值