Objective-C 字典NSDictionary用法

//
//  main.m
//  KenshinCui
//
//  Created by hxd_mac on 15-6-7.
//  Copyright (c) 2015年 hxd198. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
void Test1()
{
    //初始化字典
    //一般方式
    NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"1" forKey:@"a"];
    
    NSLog(@"dict1: %@", dict1);
    //常见方式
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"a",@"2",@"b",@"3",@"c", nil];
    
    NSLog(@"dict2: %@", dict2);
    //嵌套方式
    NSDictionary *dict3 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:@"value1",@"value2", nil] forKey:[NSArray arrayWithObjects:@"key1",@"key2", nil]];
    
    NSLog(@"dict3: %@", dict3);
    //最简单方式
    NSDictionary *dict4 = @{@"1": @"a", @"2": @"b", @"3": @"c"};
    
    NSLog(@"dict4: %@", dict4);
}
void Test2()
{
    //字典一般用法
    NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"a",@"2",@"b",@"3",@"c",@"4",@"d", nil];
    //统计个数
    NSLog(@"count: %zi", [dict1 count]);
    
    //key找value
    //第一种方式
    NSLog(@"value1: %@", [dict1 valueForKey:@"b"]);
    //第二种方式
    NSLog(@"value2: %@", dict1[@"b"]);
    //第三种方式
    NSLog(@"allValue: %@ , allKey: %@", [dict1 allValues], [dict1 allKeys]);
    
//    NSLog(@"value and key: %@", [dict1 objectForKey:[NSArray arrayWithObjects:@"a",@"e", nil]notFoundMarker: @"not found"]);
    
}
void Test3()
{
    //字典遍历三种方法
    NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"a",@"2",@"b",@"3",@"c",@"4",@"d", nil];
    
    id key;
    //常规遍历
    for (key in dict1) {
        NSLog(@"key: %@ value: %@", key, [dict1 objectForKey:key]);
    }
    //枚举快速遍历
    NSEnumerator *enumerator = [dict1 keyEnumerator];
    
    id obj = nil;
    
    while ( obj = [enumerator nextObject]) {
        NSLog(@"%@ = %@", obj, [dict1 objectForKey:obj]);
    }
    //块遍历
    [dict1 enumerateKeysAndObjectsUsingBlock: ^(id key , id obj, BOOL *stop){
        NSLog(@"key = %@, obj = %@", key, obj);
    }];
}
void Test4()
{
    //可变字典的一般用法
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"a",@"2",@"b",@"3",@"c",@"4",@"d", nil];
    //移除对象
    [dict1 removeObjectForKey:@"b"];
    
    NSLog(@"after remove:%@", dict1);
    //添加对象
    [dict1 addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"5",@"e",@"6",@"f", nil]];
    
    NSLog(@"after add: %@", dict1);
    //重置对象的值
    [dict1 setValue:@"7" forKey:@"a"];
    
    NSLog(@"after set: %@", dict1);
}
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        
        Test1();
        Test2();
        Test3();
        Test4();
    
    
    }
    return 0;
}

输出结果:

2015-06-08 20:42:49.632 KenshinCui[596:303] dict1: {

    a = 1;

}

2015-06-08 20:42:49.639 KenshinCui[596:303] dict2: {

    a = 1;

    b = 2;

    c = 3;

}

2015-06-08 20:42:49.641 KenshinCui[596:303] dict3: {

        (

        key1,

        key2

    ) =     (

        value1,

        value2

    );

}

2015-06-08 20:42:49.642 KenshinCui[596:303] dict4: {

    1 = a;

    2 = b;

    3 = c;

}

2015-06-08 20:42:49.644 KenshinCui[596:303] count: 4

2015-06-08 20:42:49.646 KenshinCui[596:303] value1: 2

2015-06-08 20:42:49.648 KenshinCui[596:303] value2: 2

2015-06-08 20:42:49.649 KenshinCui[596:303] allValue: (

    4,

    2,

    3,

    1

) , allKey: (

    d,

    b,

    c,

    a

)

2015-06-08 20:42:49.650 KenshinCui[596:303] key: d value: 4

2015-06-08 20:42:49.651 KenshinCui[596:303] key: b value: 2

2015-06-08 20:42:49.673 KenshinCui[596:303] key: c value: 3

2015-06-08 20:42:49.675 KenshinCui[596:303] key: a value: 1

2015-06-08 20:42:49.817 KenshinCui[596:303] d = 4

2015-06-08 20:42:49.818 KenshinCui[596:303] b = 2

2015-06-08 20:42:49.819 KenshinCui[596:303] c = 3

2015-06-08 20:42:49.820 KenshinCui[596:303] a = 1

2015-06-08 20:42:49.821 KenshinCui[596:303] key = d, obj = 4

2015-06-08 20:42:49.822 KenshinCui[596:303] key = b, obj = 2

2015-06-08 20:42:49.824 KenshinCui[596:303] key = c, obj = 3

2015-06-08 20:42:49.826 KenshinCui[596:303] key = a, obj = 1

2015-06-08 20:42:49.829 KenshinCui[596:303] after remove:{

    a = 1;

    c = 3;

    d = 4;

}

2015-06-08 20:42:49.831 KenshinCui[596:303] after add: {

    a = 1;

    c = 3;

    d = 4;

    e = 5;

    f = 6;

}

2015-06-08 20:42:49.833 KenshinCui[596:303] after set: {

    a = 7;

    c = 3;

    d = 4;

    e = 5;

    f = 6;

}


转载于:https://my.oschina.net/hxd198/blog/464329

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值