Objective-C 集合之NSDictionary的常用方法
2015-12-24 21:01
375 查看
OC中NSDictionary是用来存储键值对的字典,字典有两个特性:
1.无序性,字典中的元素没有顺序,存取元素必须依靠key值找到相应的元素;
2.字典中不存在相同的key值,即key值具有唯一性,但是存在不相同的key值关联相同的元素
属性
@property(readonly) NSUInteger count
得到字典中元素的个数
@property(readonly, copy) NSArray< KeyType > *allKeys
返回由字典的key值组成的数组
@property(readonly, copy) NSArray< ObjectType > *allValues
返回由字典的value值组成的数组
初始化方法
+ (instancetype)dictionary
创建空字典
+ (instancetype)dictionaryWithObjectsAndKeys:(id)
根据参数列表创建字典,顺序是value -- key,即第一个参数为value,第二个参数为key,第三个参数为value,第四个参数为key,依次类推
+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)
根据objects数组和keys数组创建字典
不同方式的初始化
初始化方法:
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"李世民",@"唐朝",@"刘邦",@"汉朝",@"秦始皇",@"秦朝", nil];
便利构造器:
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"李世民",@"唐朝",@"刘邦",@"汉朝",@"秦始皇",@"秦朝", nil];
字面量:
NSDictionary *dic2 = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3"};
使用快速枚举for (NSString *key in dictionary)得到的是字典的key值,要通过key值得到存储的对象
常用方法
- (ObjectType)objectForKey:(KeyType)
根据给出的key值得到value值
- (BOOL)isEqualToDictionary:(NSDictionary<KeyType,ObjectType> *)
判断两个字典是否相等
- (NSArray<KeyType> *)allKeysForObject:(ObjectType)
找出所有值为anObject的key,并组成一个数组返回
- (NSEnumerator *)keyEnumerator
得到由字典的key值组成的枚举器
- (NSEnumerator<ObjectType> *)objectEnumerator
得到由字典的value值组成的枚举器
- (NSArray<KeyType> *)keysSortedByValueUsingSelector:(SEL)
使用comparator比较器比较字典中的value值,并返回排序后的相应key值的数组
可变字典NSMutableDictionary
- (void)setObject:(ObjectType)
添加key值为aKey,value值为anObject的数据
- (void)removeObjectForKey:(KeyType)
移除key值为aKey的数据
- (void)removeObjectsForKeys:(NSArray<KeyType> *)
移除由keyArray的元素指定的key的所有数据
- (void)removeAllObjects
移除所有的数据
- (void)addEntriesFromDictionary:(NSDictionary<KeyType,ObjectType> *)
添加字典otherDictionary
- (void)setDictionary:(NSDictionary<KeyType,ObjectType> *)
设置字典内容为otherDictionary
[b]转载请注明:作者SmithJackyson[/b]
1.无序性,字典中的元素没有顺序,存取元素必须依靠key值找到相应的元素;
2.字典中不存在相同的key值,即key值具有唯一性,但是存在不相同的key值关联相同的元素
属性
@property(readonly) NSUInteger count
得到字典中元素的个数
@property(readonly, copy) NSArray< KeyType > *allKeys
返回由字典的key值组成的数组
@property(readonly, copy) NSArray< ObjectType > *allValues
返回由字典的value值组成的数组
初始化方法
+ (instancetype)dictionary
创建空字典
+ (instancetype)dictionaryWithObjectsAndKeys:(id)
firstObject,
...
根据参数列表创建字典,顺序是value -- key,即第一个参数为value,第二个参数为key,第三个参数为value,第四个参数为key,依次类推
+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)
objectsforKeys:(NSArray<id<NSCopying>> *)
keys
根据objects数组和keys数组创建字典
不同方式的初始化
初始化方法:
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"李世民",@"唐朝",@"刘邦",@"汉朝",@"秦始皇",@"秦朝", nil];
便利构造器:
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"李世民",@"唐朝",@"刘邦",@"汉朝",@"秦始皇",@"秦朝", nil];
字面量:
NSDictionary *dic2 = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3"};
使用快速枚举for (NSString *key in dictionary)得到的是字典的key值,要通过key值得到存储的对象
常用方法
- (ObjectType)objectForKey:(KeyType)
aKey
根据给出的key值得到value值
- (BOOL)isEqualToDictionary:(NSDictionary<KeyType,ObjectType> *)
otherDictionary
判断两个字典是否相等
- (NSArray<KeyType> *)allKeysForObject:(ObjectType)
anObject
找出所有值为anObject的key,并组成一个数组返回
- (NSEnumerator *)keyEnumerator
得到由字典的key值组成的枚举器
- (NSEnumerator<ObjectType> *)objectEnumerator
得到由字典的value值组成的枚举器
- (NSArray<KeyType> *)keysSortedByValueUsingSelector:(SEL)
comparator
使用comparator比较器比较字典中的value值,并返回排序后的相应key值的数组
可变字典NSMutableDictionary
- (void)setObject:(ObjectType)
anObjectforKey:(id<NSCopying>)
aKey
添加key值为aKey,value值为anObject的数据
- (void)removeObjectForKey:(KeyType)
aKey
移除key值为aKey的数据
- (void)removeObjectsForKeys:(NSArray<KeyType> *)
keyArray
移除由keyArray的元素指定的key的所有数据
- (void)removeAllObjects
移除所有的数据
- (void)addEntriesFromDictionary:(NSDictionary<KeyType,ObjectType> *)
otherDictionary
添加字典otherDictionary
- (void)setDictionary:(NSDictionary<KeyType,ObjectType> *)
otherDictionary
设置字典内容为otherDictionary
[b]转载请注明:作者SmithJackyson[/b]
相关文章推荐
- Objective - C tableView自适应高度 & KVC (Key -Value-Coding)
- Objective-C tableView自定义cell
- How to call an Objective-C Method from a C Method?
- Objective - C UITableView界面传值 从前往后,从后往前
- Objective - C UITableView学习笔记
- 【转】Android自动化测试(UiAutomator)——UiObject
- java线程同步:使用Object的wait,notify,notifyAll做线程调度
- Objective--C 导航视图控制器
- Objective-C基础1-类目(Category)和延展(Extension)的区别
- Objective-C 集合之NSArray的常用方法
- Objective-C NSObject 的实现分析(2014-10-23更新)
- Objective-C Runtime 总结:消息机制 篇
- Object.ReferenceEquals
- 遍历系统的所有ObjectType和TypeIndex
- iOS开发系列--Objective-C之KVC、KVO
- Objective-c语言_内存管理2(代码)
- Objective-C 和 Core Foundation 对象相互转换的内存管理总结
- Ubuntu snmpwalk 执行报错 “Unknown Object Identifier”
- ActiveX控件之ActiveXObject is not defined
- JSONObject、JSONArray