OC中NSDictionary、NSMutableDictionary、NSSet、NSMutableSet

本文介绍了Objective-C中的 NSDictionary 和 NSMutableDictionary 的使用,包括字典的创建、读取、遍历及文件存储。同时,也探讨了 NSSet 和 NSMutableSet 的特性,NSSet 是不可变的无序集合,而 NSMutableSet 支持元素的增删操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSDictionary

字典是一个无序的集合,它使通过键值对的方式来存储数据的
key是有要求 NSCopying才能成为字典key,一般情况都是使用字符串作为字典的key
NSDictionary 不可变得字典,一经创建里面的元素就不可以改变了

NSDictionary的创建与读取

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

        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@10,@"age",@"张三",@"name", nil];

        NSLog(@"%@",dict);
        //      取出某个键所对应的值
        NSNumber *number = [dict objectForKey:@"age"];
        NSLog(@"%@",number);
        //      简便的取值方式(常用)
        NSString *name = dict[@"name"];
        NSLog(@"%@",name);

        //      使用简便方式来创建字典
        NSDictionary *dict1 = @{@"age":@20,@"name":@"apple",@"height":@1.8};
        NSNumber *age1 = dict1[@"age"];
        NSLog(@"%@",age1);
        NSLog(@"%@",dict1);

        //      取出字典长度,字典中有几个键值对
        long  len = dict1.count;
        len = [dict1 count];
        NSLog(@"%zd",len);

    }
    return 0;
}

NSDictionary的遍历

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //      使用简便方式来创建字典
        NSDictionary *dict1 = @{@"age":@20,@"name":@"apple",@"height":@1.8};

        //    第一种方式:先取出字典中的keys,然后使用数组的方式对keys遍历
        NSArray *keys =  dict1.allKeys;
        //     使用数组方式来遍历keys
        for (NSString *key in keys) {
            NSLog(@"%@ = %@",key,dict1[key]);

        }
           第二种方式,使用增强for(掌握)
        for (NSString *key in dict1) {
            NSLog(@"%@ = %@",key,dict1[key]);
        }
        //    第三种方式 枚举器(不常用)
        NSEnumerator *keyEnumerator =  dict1.keyEnumerator;
        NSString *key = nil;
        while ((key = keyEnumerator.nextObject)) {
            NSLog(@"%@ = %@",key,dict1[key]);
        }
        //     取出字典中所有值
        NSArray *values  = dict1.allValues;
        for (id value in values) {
            NSLog(@"%@",value);
        }
        //     第四种 使用block的方式
        [dict1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            NSLog(@"%@ : %@",key,obj);
        }];
    }
    return 0;
}

NSDictionary的文件存储

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //      使用简便方式来创建字典
        NSDictionary *dict1 = @{@"age":@20,@"name":@"apple",@"height":@1.8};
        //      字典写文件
        NSString *path = @"/Users/zzj/Desktop/测试.txt";
        //      如果返回YES表示写入成功
        BOOL isSuccess = [dict1 writeToFile:path atomically:YES];
        if(isSuccess){
            NSLog(@"写入成功!");
        }
        //      从文件中读取字典
        NSDictionary *dict  =  [NSDictionary dictionaryWithContentsOfFile:path];
        NSLog(@"%@",dict);
    }
    return 0;
}

NSMutableDictionary

是NSDictionary的子类,它是可变的字典,它支持增删除改的操作

NSMutableDictionaryde创建修改删除

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //     创建一个可变的字典
        //     创建一个空字典
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        //     创建一个有容量的字典
        NSMutableDictionary *dict1  = [NSMutableDictionary  dictionaryWithCapacity:10];
        NSLog(@"%zd,%zd",dict.count,dict1.count);
        dict1.dictionary = @{@"age":@10,@"name":@"lisi",@"height":@1.8};
        NSLog(@"%@",dict1);
        //     插入元素,如果字典有这个key就是修改,如果没有就插入
        //     第一种方式
        [dict1 setObject:@50.0  forKey:@"weight"];
        //      第二种方式
        dict1[@"sex"] = @"男";
        //      修改
        dict1[@"age"] = @30;
        NSLog(@"%@",dict1);
        //       删除
        [dict1 removeObjectForKey:@"age"];
        //       删除多个元素,数组中存储是你要删除的key
        [dict1 removeObjectsForKeys:@[@"name",@"sex"]];
        //       清空字典
        [dict1 removeAllObjects];
    }
    return 0;
}

NSSet

无序,元素不可以重复,可以用快速去除数组中的重复元素,效率比数组要高
NSSet:特点:不可以变,一经创建不能增删改

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSSet *set = [NSSet setWithObjects:@1,@2,@3,@4,@1,nil];
        NSLog(@"%@",set);
        //        NSSet *set1 = @[@1,@2,@3]; 错误的
        //      遍历
        for (NSNumber *number in set) {
            NSLog(@"%@",number);
        }
        //      判断是否包含某个元素
        BOOL isContains  = [set containsObject:@2];
        if (isContains) {
            NSLog(@"包含!");
        }

        //    给数组去重
        NSArray *array = @[@1,@2,@3,@4,@1];
        //      通过数组创建一个set
        NSSet *set1 = [NSSet setWithArray:array];
        //      把Set转换为数组
        NSArray *arr  =  [set1 allObjects];
        NSLog(@"%@",arr);
    }
    return 0;
}

NSMutableSet

NSMutableSet:可以增删,没有改

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSSet *set = [NSSet setWithObjects:@1,@2,@3,@4,@1,nil];
        //      创建
        //      NSMutableSet *setM = [NSMutableSet set];
        NSMutableSet *setM = [NSMutableSet setWithCapacity:10];
        setM.set = set;
        //      添加元素
        [setM addObject:@10];
        //       删除元素
        [setM removeObject:@1];
        //      删除所有元素
        [setM removeAllObjects];
        NSLog(@"%@",setM);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值