代码改变世界

NSDictionary

2015-10-15 20:06  xiangjune  阅读(166)  评论(0编辑  收藏  举报

// ------------------------------字典-------------------------------------------------

    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"111", @"one", @"222", @"two", @"333", @"three", nil];

    NSLog(@"dic count=%ld", [dic1 count]);

    for (id key in dic1)

    {

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

    }

    

    // 可变字典

    NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithCapacity:1];

    [dic2 setObject:@"111" forKey:@"one"];

    [dic2 setObject:@"222" forKey:@"two"];

    [dic2 setObject:@"333" forKey:@"three"];

    [dic2 setObject:@"444" forKey:@"four"];

    [dic2 setObject:@"555" forKey:@"five"];

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

    [dic2 removeObjectForKey:@"three"];

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

    

    NSLog(@"dic allkeys = %@", [dic2 allKeys]);

    NSLog(@"dic allValues = %@", [dic2 allValues]);