1    NSSet和NSMutableSet
NSSet和NSArray一样,也是一个集合,但是集合里的多个对象没有明显的顺序。NSSet不允许包含相同的元素,如果试图把两个相同的元素放在同一个NSSet集合中,则会只保留一个元素。NSMutableArray代表集合元素可变的NSSet集合,在NSSet的基础上增加了添加、删除的方法。

// set的基本使用  
void test()  
{  
    //"jack"只添加了一个  
    NSSet *s = [NSSet setWithObjects:@"jack",@"rose", @"jack",@"jack3",nil];  
    NSLog(@"%@", s);      
      
    NSSet *s2 = [NSSet setWithObjects:@"jack",@"rose", @"jack2",@"jack3",nil];  
      
    // 随机拿出一个元素  
    NSString *str =  [s2 anyObject];  
      
    NSLog(@"%@", str);  
      
    NSLog(@"%ld", s2.count);  
}  
  
int main()  
{  
    NSMutableSet *s = [NSMutableSet set];  
      
    // 添加元素  
    [s addObject:@"hack"];  
    [s addObject:@"hasddfg"];    
      
    // 删除元素  
    [s removeObject:@"hasddfg"];  
    return 0;  
}  

  NSDictionary和NSMutableDictionary
NSDictionary用于保存具有映射关系的数据,因此,NSDictionary集合里保存着两组值,一组值用于保存key,一组值用于保存value。key和value可以是任何引用类型的数据,key不允许重复。NSMutableDictionary继承了NSDictionary,它代表一个key-value可变的NSDictionary集合。

 
void use()  
{  
    /*创建NSDictionary的几种方法 
     
     NSDictionary *dict = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"]; 
     
     
     NSArray *keys = @[@"name", @"address"]; 
     NSArray *objects = @[@"jack", @"北京"]; 
     
     NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 
     
     
     
     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: 
     @"jack", @"name", 
     @"北京", @"address", 
     @"32423434", @"qq", nil]; 
     
    */  
      
    //快速创建NSDictionary  
    NSDictionary *dict = @{@"name" : @"jack", @"address" : @"北京"};  
      
      
    // id obj = [dict objectForKey:@"name"];  通过key,找到value  
    id obj = dict[@"name"];  //快速访问  
    NSLog(@"%@", obj);  
      
      
      
    // 返回的是键值对的个数  
    NSLog(@"%ld", dict.count);  
}  
  
void use2()  
{  
    //只能快速创建NSDictionary,不能快速创建NSMutableDictionary ,会警告。如果运行时,修改Dictionary会出错  
   // NSMutableDictionary *dict = @{@"name" : @"jack"};  
      
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];  
      
    // 添加键值对,不能添加相同的key,只会添加一个“name”  
    [dict setObject:@"jack" forKey:@"name"];        
    [dict setObject:@"北京" forKey:@"address"];      
    [dict setObject:@"rose" forKey:@"name"];  
      
     NSLog(@"%@",dict);  
      
    // 移除键值对  
    // [dict removeObjectForKey:<#(id)#>];  
          
    NSString *str = dict[@"name"];  
         
    NSLog(@"%@", str);  
  
    // 字典不允许有相同的key,但允许有相同的value(Object)  
    // 字典的无序的  
    NSDictionary *dict = @{  
    @"address" : @"北京",  
    @"name" : @"jack",  
    @"name2" : @"jack",  
    @"name3" : @"jack",  
    @"qq" : @"7657567765"};  
  
    //遍历NSDictionary  
      
    //    NSArray *keys = [dict allKeys];  
    //  
    //    for (int i = 0; i<dict.count; i++)  
    //    {  
    //        NSString *key = keys[i];  
    //        NSString *object = dict[key];  
    //  
    //  
    //        NSLog(@"%@ = %@", key, object);  
    //    }  
      
    //系统自带的遍历NSDictionary方法        
    [dict enumerateKeysAndObjectsUsingBlock:  
     ^(id key, id obj, BOOLBOOL *stop) {  
         NSLog(@"%@ - %@", key, obj);  
           
         // *stop = YES;  
     }];  
  
}  

 3    NSNumber和NSValue
NSNumber和NSValue都是包装类,NSValue是NSNumber的父类。NSValue代表更通用的包装类,用于包装short、int、long、float、char、指针、结构体、对象id等。NSNumber是更具体的包装类,主要用于包装C语言的各种数值类型 ,这样基本的数值类型就能添加到集合中了。NSNumber提供了实例方法和类方法用于包装基本数值类型,以及简化写法。
+ numberWithXxx: 类方法
- initWithXxx: 实例方法
@1;@12.3; 简化写法

void test()  
{  
    NSNumber *num = [NSNumber numberWithInt:10];  
      
    NSDictionary *dict =  @{  
    @"name" : @"jack",  
      
      
    @"age" : num  
      
    };  
      
    NSNumber *num2 = dict[@"age"];  
      
      
    int a = [num2 intValue];  
      
    NSLog(@"%d" , a);  
}  
  
int main()  
{  
    // @20  将 20包装成一个NSNumber对像  
      
      
    NSArray *array = @[  
      
    @{@"name" : @"jack", @"age" : @20},  
      
    @{@"name" : @"rose", @"age" : @25},  
      
    @{@"name" : @"jim", @"age" : @27}  
    ];  
      
      
    // 将各种基本数据类型包装成NSNumber对象  
    @10.5;  
    @YES;  
    @'A'; // NSNumber对象  
      
    @"A"; // NSString对象  
      
      
      
    // 将age变量包装成NSNumber对象  
    int age = 100;  
    @(age);  
    //[NSNumber numberWithInt:age];  
      
      
    NSNumber *n = [NSNumber numberWithDouble:10.5];  
      
      
    int d = [n doubleValue];  
    //d = [n intValue];  
      
    NSLog(@"d=%d",d);  
      
      
    int a = 20;  
      
    // @"20"  
    NSString *str = [NSString stringWithFormat:@"%d", a];  
    NSLog(@"%d", [str intValue]);  
  
    // 结构体--->OC对象  
      
    CGPoint p = CGPointMake(10, 10);  
    // 将结构体转为Value对象  
    NSValue *value = [NSValue valueWithPoint:p];  
      
    // 将value转为对应的结构体  
    // [value pointValue];  
      
    NSArray *array = @[value ];  
      
      
    return 0;