OCdemo - 05 NSDictionary 字典

//
//  main.m
//  OCdemo-05
//
//  Created by lanou3g on 15/10/13.
//  Copyright (c) 2015年 Object. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        /*
        
    // NSDictionary  字典
    //1.创建字典对象
        //使用初始化方法
        NSDictionary *dict1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"object1",@"key1",@"object2",@"key2",@"object3",@"key3", nil];
        NSLog(@"%@",dict1);
        
        NSArray *objectArray = @[@"jiangpengsheng",@"baocheng",@"laohan",@"dahuang"];
        NSArray *keyArray = @[@"jiang",@"yue",@"han",@"yellow"];
        NSDictionary *dict2 = [[NSDictionary alloc]initWithObjects:objectArray forKeys:keyArray];
        NSLog(@"%@",dict2);
        
        // 使用类方法创建对象
        NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"object1",@"key1",@"object2",@"key2",@"object3",@"key3", nil];
        NSLog(@"%@",dict3);
        
        NSArray *objectArray1 = @[@"dao",@"ruai",@"mi",@"fa",@"sao",@"la",@"xi"];
        NSArray *keyArray1 = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"];

        NSDictionary *dict4 = [NSDictionary dictionaryWithObjects:objectArray1 forKeys:keyArray1];
        NSLog(@"%@",dict4);
        
        // 字面量方法
        NSDictionary *dict5 = @{
                                @"ren":@"renyusheng",
                                @"lu":@"luwenke",
                                @"xu":@"xujin",
                                @"ren":@"renyuting" //key 值不可以重复,所以此行 加不进去。 object 值可以重复
                                };
        NSLog(@"%@",dict5);
        
        //2.查找
        //  根据 key 值 查找  object
        NSLog(@"%@",[dict2 objectForKey:@"jiang"]);
        //使用下标从字典中取出对象
        NSLog(@"%@",dict3[@"key1"]);
        
        //3. 获取字典的长度
        NSUInteger length = [dict5 count];
        NSLog(@"%lu",length);
        //获取所有的key值
        NSArray *keyarray = [dict4 allKeys];
        NSLog(@"%@",keyarray);
        //获取所有的 value 值
        NSArray *valueArray = [dict4 allValues];
        NSLog(@"%@",valueArray);
        
        
        // * 4.字典遍历
        NSArray *array1 = [dict1 allKeys];
        for (int i = 0; i < array1.count; i++) {
            NSLog(@"%@",[dict1 objectForKey:array1[i]]);
        }
        
        NSArray *array2 = [dict2 allKeys];
        for (int i = 0; i < array2.count; i++) {
            NSLog(@"%@",[dict2 objectForKey:array2[i]]);
        }
        NSArray *array3 = [dict3 allKeys];
        for (NSString *key in array3) {
//            NSLog(@"%@",dict3[key]);
            NSLog(@"%@",[dict3 objectForKey:key]);
        }
        
        NSMutableArray *marray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
        for (int i = 0; i < marray.count; i++) {
            marray[i] = @"1";
            NSLog(@"%@",marray[i]);
        }
        
        //通过 for - in 遍历不能修改数组的值
        for (NSString *string in marray) {
            marray[0] = @"3";
            NSLog(@"%@",string);
        }
        */
        
        /*
//        NSMutableDictionary  可变字典,是 NSDictionary 的子类
        //1.字典定义
        NSMutableDictionary *mdict1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"xiaoqiang",@"wangba",@"wangbowen",@"wang",@"zhusong",@"zhu",@"zhengzheng",@"taiyang", nil];
        NSLog(@"%@",mdict1);
        
        //2.添加,修改 (如果字典中不存在 key值时,添加。key值存在时 ,修改)
        [mdict1 setObject:@"chenjun" forKey:@"chen"];
        NSLog(@"%@",mdict1);
        [mdict1 setValue:@"wangshuchao" forKey:@"shu"];
        NSLog(@"%@",mdict1);
        
        //修改
        [mdict1 setObject:@"laowang" forKey:@"wangba"];
        NSLog(@"%@",mdict1);
        
        // setValue forKey 中key的参数类型只能是 NSString,
        // setObject forKey 中key的参数类型可以是任意对象
        // setObject forKey 是 NSMutableDictionary 特有的方法
        // setValue forKey 是 NSObject 对象的方法,也就是所有OC对象都有这个方法
        
        //3.删除
        //删除一个对象
        [mdict1 removeObjectForKey:@"taiyang"];
        NSLog(@"%@",mdict1);
        //删除所有对象
        [mdict1 removeAllObjects];
        NSLog(@"%@",mdict1);
        
        */
        
        //  NSSet 集合(元素不可以重复,无序存放)
        //  创建一个集合
        NSSet *set = [NSSet setWithObjects:@"T",@"L",@"L",@"D",@"P",@"B", nil];
        NSLog(@"%@",set);
        
        //随机取出一个元素
        NSString *str = [set anyObject];
        NSLog(@"%@",str);
        
        //取出所有的元素
        NSArray *array = [set allObjects];
        NSLog(@"%@",array);
        
        // 获取集合的个数
        NSLog(@"%lu",[set count]);// 5 ,第二个L重复,没有存入
        
        //判断对象是否在集合中
        BOOL isExist = [set containsObject:@"L"];
        NSLog(@"%d",isExist);
        //---------------------------------------------------------------------
        // NSMutableSet  可变集合
        // 创建对象
        NSMutableSet *mSet = [NSMutableSet setWithObjects:@"shifeng",@"xiaoyi",@"jianjian",@"jianjian",@"wenqi", nil];
        NSLog(@"%@",mSet);
        
        // 添加对象
        [mSet addObject:@"haohao"];
        NSLog(@"%@",mSet);
        // 删除对象
        [mSet removeObject:@"jianjian"];
        NSLog(@"%@",mSet);
        
        // NSCountedSet 能记录元素的重复次数
        NSCountedSet *countedSet = [NSCountedSet setWithArray:@[@"cunzhang",@"huangshang",@"shuaishuai",@"xiaoqiangzi",@"jinjin",@"jinjin"]];
        
        NSUInteger count = [countedSet countForObject:@"jinjin"];
        NSLog(@"count = %lu",count);
        
        // 快速遍历
        for (id obj in countedSet) {
            NSLog(@"%@",obj);
        }
        for (id obj1 in mSet) {
            NSLog(@"%@",obj1);
        }
        
        
        
        
    }
    return 0;
}

你可能感兴趣的:(OCdemo - 05 NSDictionary 字典)