Foundation--NSMutableDictionary

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
        //创建空字典,有意义
        NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:100];
        //预测字典有100个键值对
        NSMutableDictionary *dict3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];
       //标准方法
//        NSMutableDictionary *dict4 = @{@"1":@"one", @"2":@"two", @"3":@"three"};
        //会退化成不可变字典

        //添加方法
        [dict3 setValue:@"four" forKey:@"4"];//添加一个
        NSLog(@"%@", dict3);
        NSDictionary *added = @{@"5":@"five", @"6":@"six", @"7":@"seven"};
        [dict3 addEntriesFromDictionary:added];
        NSLog(@"%@", dict3);

        //覆盖方法
        NSDictionary *dict5 = @{@"1":@"one", @"2":@"two", @"3":@"three"};
        NSMutableDictionary *dict6 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"aaa",@"1",@"bbb",@"2",@"ccc",@"3", nil];
        [dict6 setDictionary:dict5];
        NSLog(@"%@", dict6);

        //删除方法
        [dict3 removeObjectForKey:@"1"];//删除指定关键字对应的键值对
        NSLog(@"%@", dict3);
        NSArray *del = @[@"3", @"5"];
        [dict3 removeObjectsForKeys:del];
        NSLog(@"%@", dict3);//批量删除
        [dict3 removeAllObjects];
        NSLog(@"%lu", dict3.count);//清空字典
     }
    return 0;
}
阅读更多
文章标签: foundation
个人分类: Foundation
上一篇Foundation之NSDictionary
下一篇沙箱机制
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭