字典类

来源:互联网 发布:淘宝cdk抽皮肤是真的吗 编辑:程序博客网 时间:2024/06/05 06:32

简介:

字典用于保存具有映射关系 (key - value对) 数据的集合. 一个key-value对认为是一个条目 (Entry) , 字典是存储key-value对的容器.


特点:

1, 靠key存取元素.

2, key不能重复, value必须是对象.

3, 键值对在字典中是无序存储的.


划分:

1, 不可变字典 (NSDictionary) 

2, 可变字典 (NSMutableDictionary)



不可变字典 (NSDictionary) :

字典一旦创建, 键值对就不可更改, 不可添加, 不可删除. 仅能读取key或者value.


继承关系 :

@interface NSDictionary : NSObject

常用方法 :

创建不可变字典对象的方法 (Creating a Dictionary) :

// + (instancetype)dictionary

NSDictionary *dic1 = [NSDictionary dictionary];

// + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...

NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];



不可变字典的初始化方法 (Initializing an NSDictionary Instance) :

// - (instancetype)init

NSDictionary *dic3 = [[NSDictionary alloc]init];

// - (instancetype)initWithObjectsAndKeys:(id)firstObject, ...

NSDictionary *dic4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
</pre><p></p><span style="font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px">( 所有不可变的量只能在初始化的时候赋值 )</span><br style="font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px" /><p style="word-wrap:break-word; margin-top:0px; margin-bottom:5px; font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px"></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:5px; font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px"></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:5px; font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px"><span style="font-family:'Microsoft YaHei'; color:rgb(165,42,42)"></span></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:5px; font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px"><span style="font-family:'Microsoft YaHei'; color:rgb(165,42,42)">不可变字典通过</span><span style="font-family:'Microsoft YaHei'; color:rgb(165,42,42)">key</span><span style="font-family:'Microsoft YaHei'; color:rgb(165,42,42)">值查询</span><span style="font-family:'Microsoft YaHei'; color:rgb(165,42,42)">value的方法 (</span><span style="color:rgb(165,42,42); font-family:'Microsoft YaHei'">Accessing Keys and Values)</span><span style="font-size:18px; font-family:'Microsoft YaHei'; color:rgb(165,42,42)"><span style="font-size:16px"> </span><span style="font-size:16px">:</span></span></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:0px; font-size:18px; font-family:Menlo; color:rgb(29,148,33)">// - (id)objectForKey:(id)aKey</p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:0px; font-size:18px; font-family:Menlo"></p><pre name="code" class="objc">NSString *str = [dic2 objectForKey:@"key1"];



可变字典 (NSMutableDictionary) :

可以对管理的键值对进行增, 删, 改.


继承关系 :

@interface NSMutableDictionary : NSDictionary
</pre><p></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:5px; font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px"></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:5px; font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px"><span style="font-family:'Microsoft YaHei'">常用方法:</span></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:5px; font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px"><span style="font-family:'Microsoft YaHei'; color:rgb(165,42,42)">可变字典添加键对值的方法的方法</span><span style="font-family:'Microsoft YaHei'; color:rgb(165,42,42)"> (Adding Entries to a Mutable Dictionary) : </span><span style="font-size:14px; font-family:Arial,Helvetica,sans-serif">​</span></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:5px; font-family:微软雅黑,'Microsoft Yahei','Helvetica Neue','Hiragino Sans GB',宋体,simsun,黑体,Arial,sans-serif; font-size:16px; line-height:24px"><span style="color:rgb(29,148,33); font-family:Menlo; font-size:18px">// - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey        </span></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:0px; font-size:18px; font-family:Menlo"></p><pre name="code" class="objc">[mDic1 setObject:@"value3" forKey:@"key3"];

// - (void)setValue:(id)value forKey:(NSString *)key

        ​// 因为value值不能为空,删除一对键值对也可以用添加value值为nil的方式       

[mDic1 setValue:nil forKey:@"key3"];

可变字典删除键值对的方法 (Removing Entries From a Mutable Dictionary) :

// - (void)removeObjectForKey:(id)aKey        

[mDic1 removeObjectForKey:@"key3"];


// - (void)removeAllObjects

        // 删除全部的键值对       

[mDic1 removeAllObjects];

0 0
原创粉丝点击