字典

/*

     

     不可变字典 NSDiction

     */

    // 创建字典对象

    // 参数1 : value

    //  参数2 : key 值

//    NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"张三" forKey:@"name"];

//    NSLog(@"dic1%@",dic1);

    

//    // 创建有多个 Key - value 的字典

//    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"1",@"3",@"4",@"1",@"1",nil];

//    NSLog(@"dic2 : %@",dic2);

////    dic2 : {

////        2 = 1;// 后面是: Key;

////        4 = 3;

////    }

//    

//    // Key 值是唯一的  不能重复

//    

////    NSDicction  NSset[1249:76650] dic2 : {

////        1 = 1;

////        2 = 1;

////        4 = 3;

////    }

//    

//    

//    

//    // 获取所有的 KEY

//    NSLog(@"key%@",dic2.allKeys);

////    key(

////        2,

////        4

////        )

//

//    

//    

//    // 获取所有的 Value

//    NSLog(@"values %@",dic2.allValues);

////    values (

////            1,

////            3

////            )

//

//    

//    

//    // 根据 KEY 获取 value

//    

//    

//    // 字典访问是: ObjectForkey

//    // 数组访问:   ObjectAtIndex

 

    

//    NSLog(@"objec:%@",[dic2  objectForKey:@"2"]);// objec:1

    

    

    /*

     

     可变字典  NSMutableDictionary

     */

    // 创建

//    NSMutableDictionary  *mDic = [NSMutableDictionary dictionary];

//    // 添加 Key- Value 对

//    // setObject:orKey:(即有添加,还有(有 Key):替换功能 ,修改)

//    // 参数1 :(value)

//    // 参数2 : key

//    

//    // 当 Key 不存在时.可以添加 kEY-value 值

//    // 当 Key 存在时 找到对应的Key 替换对应 Value

//    // 数组添加: addObject:

//    [mDic setObject:@"lielei" forKey:@"name"];

//    [mDic  setObject:@"male" forKey:@"sex"];

//    NSLog(@"mDic:%@",mDic);

////    mDic:{

////        name = lielei;

////        sex = male;

////    }

//    [mDic setObject:@"hanmeimie" forKey:@"name"];

//    NSLog(@"mDic:%@",mDic);

////mDic:{

////    name = hanmeimie;

////    sex = male;

////}

//

//    

//    // 删除操作

//    [mDic removeObjectForKey:@"name"];

    

    

    

    

    

    

    

    //     NSLog(@"mDic:%@",mDic);

////    NSset[1292:82666] mDic:{

////        sex = male;

////    }

//    

    

    // 遍历字典

//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"leilei",@"name",@"male",@"sex",@"18",@"age",nil];

//    NSLog(@"dic:%@",dic);

//    for (NSInteger i = 0; i < dic.allKeys.count; i++) {

//        // 获取每一个 key

//        NSString *key = [dic.allKeys objectAtIndex:i];

//        // 根据 key 获取对应的 value

//        NSLog(@"key:%@,value:%@",key,[dic objectForKey:key]);

//    }

// 打印:   {

//        age = 18;

//        name = leilei;

//        sex = male;

//    }

//    2015-02-05 15:48:56.452 OC_6 NSDicction  NSset[1338:88920] key:name,value:leilei

//    2015-02-05 15:48:56.452 OC_6 NSDicction  NSset[1338:88920] key:sex,value:male

//    2015-02-05 15:48:56.452 OC_6 NSDicction  NSset[1338:88920] key:age,value:18

//    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"lianxue",@"name",@"f",@"sex", nil];

//    NSLog(@"dic1:%@",dic1);

//    for (NSInteger i = 0; i < dic1.allKeys.count; i++) {

//        NSString *key = [dic1.allKeys objectAtIndex:i];

//        NSLog(@"key:%@ value:%@",key, [dic1 objectForKey:key ]);

//    }

//    

    /*

     练习

     1.数组套数组

     2.字典套字典(某个 value 是字典)

     3.数组套字典

     4,字典套数组(某个 value 是 数组)

     */

    // 数组套数组:

//    NSMutableArray *arr = [NSMutableArray array];

//    NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"xubin",@"liuqingchun", nil];

//    [arr addObject:arr1];

//    [arr addObject:@"haolianxue"];

//    NSLog(@"arr:%@",arr);

//    NSArray *a1 = [NSArray arrayWithObjects:@"nihao",arr, nil];

//    NSLog(@"%@",a1);

//    

    // 字典套字典

//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"leilei",@"name",@"male",@"sex",@"18",@"age",nil];

//    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"a",dic,@"dictionary", nil];

//    NSLog(@"dic2%@",dic2);

//    dic2{

//        a = 1;

//        dictionary =     {

//            age = 18;

//            name = leilei;

//            sex = male;

//        };

//    }

    

//    NSMutableArray *a = [NSMutableArray array];

//    NSMutableDictionary *d = [NSMutableDictionary dictionary];

//    [d ]

//    [a addObject:d];

    

  

    

//    // 数组套字典

//    NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"xubin",@"liuqingchun", nil];

//    NSArray *arr3 = [NSArray arrayWithObjects:arr1,dic2, nil];

//    NSLog(@"%@",arr3);

//    (

//     xubin,

//     liuqingchun

//     ),

//    {

//        a = 1;

//        dictionary =         {

//            age = 18;

//            name = leilei;

//            sex = male;

//        };

//    }

//    )

    

    /*

     数组

     for ((<#type *object#> in <#collection#>))

     type * 从集合中获取元素类型

     object 给获取到的对象起一个临时变量名

     collection 集合(数组/字典/集合)

     */

    // 数组快速遍历得到某个元素

//    NSArray *arr = [NSArray arrayWithObjects:@"iPhone",@"德玛西亚",@"翡翠梦境",@"龙之谷",@"blookStrike", nil];

//    

//    for (NSString *temp in arr) {

//        NSLog(@"%@",temp);

//    }

//    NSArray *b = [NSArray arrayWithObjects:@"jianren",@"xunbin" ,nil];

//    for (NSString * temp in b) {

//        NSLog(@"%@",temp);

//    }

    

    // 字典

//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"2",@"3",@"4",nil];

//    // 对字典遍历得到的时 key 值.通过 key 值可以访问到对应的 value 值

//    for (NSString *key in dic) {

//        NSLog(@"%@ = %@",key,[dic objectForKey:key]);

//    }

//    

    

//    for (NSString *key in dic) {

//        NSLog(<#NSString *format, ...#>)

//    }

    

    

    

    /*

     集合特性 元素唯一 无序 元素必须是对象

     */

    

//    //  创建对象

//    NSSet *set = [NSSet setWithObjects:@"a",@"p",@"p" ,nil];

//    //NSset *set = [NSSet setWithObjects:@"a",@"p",@"p" ,nil];

//    NSLog(@"set1:%@",set);

//    // 集合元素个数

//

//    NSLog(@"count %ld",set.count);

//    // 获取摸个元素

//    NSLog(@"object%@",[set  anyObject]);

//    // 判断是否包含摸个元素

//    

//    if ([set containsObject:@"a"]) {

//        

//        NSLog(@"集合中包含该元素");

//    }

    /*

     可变集合  NSMutaleset

     

     

     */

//    // 创建集合

//    NSMutableSet  *mSet = [NSMutableSet  set];

//    //添加 元素

//    [mSet addObject:@"i"];

//    [mSet addObject:@"o"];

//    [mSet addObject:@"s"];

//    NSLog(@"mSet%@",mSet);

//    // 删除

//    [mSet removeObject:@"i"];

//    NSLog(@"mSet%@",mSet);

    

    /*

     技术集合

     */

    

    NSCountedSet *cSet = [NSCountedSet set];

    // 添加元素

    [cSet addObject:@"a"];

    [cSet addObject:@"p"];

    [cSet addObject:@"p"];

    

    NSLog(@"cSet %@",cSet);

   //  计数器 :cSet<NSCountedSet: 0x1002022f0> (a [1], p [2]) [1]被添加次数

    NSLog(@"cSet %ld",cSet.count);//2

    // 元素再集合中被添加的次数

    

    NSLog(@"count %ld",[cSet countForObject:@"p"]);

    // 集合快速遍历

    for (NSString *temp in cSet) {

        NSLog(@"temp:%@",temp);

    }

//    2015-02-06 11:10:32.242 OC_6 NSDicction  NSset[790:38933] temp:a

//    2015-02-06 11:10:32.242 OC_6 NSDicction  NSset[790:38933] temp:p

    

    

    NSArray *arr = [NSArray arrayWithObjects:@"jianing",@"liuqingchun",@"haolianxue",nil];

    // 排序方法

    // SEL 方法类型

    // @selector 方法选择器

    SEL mothod = @selector(compare:);

    NSArray *newArr = [arr sortedArrayUsingSelector:mothod];

    NSLog(@"arr %@",newArr);

//    (

//     haolianxue,

//     jianing,

//     liuqingchun

//     )

 

    // 可变数据排序

    

    NSMutableArray *mArr = [NSMutableArray arrayWithArray:arr];

    [mArr sortUsingSelector:@selector(compare:)];

    NSLog(@"nArr %@",mArr);

//    (

//     haolianxue,

//     jianing,

//     liuqingchun

//     )

//    

    return 0;

}

posted @ 2015-03-01 16:25  被阳光素描  阅读(125)  评论(0编辑  收藏  举报