关闭

Foundation--NSMutableDictionary

标签: foundation
21人阅读 评论(0) 收藏 举报
分类:
#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;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:714次
    • 积分:41
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:3篇
    • 译文:1篇
    • 评论:0条
    文章分类
    文章存档