Objective-C ---NSDictionary(梳理整理)
2017-04-15 00:00
288 查看
摘要: 对oc中常用集合 字典 使用进行整理
key->value键值对,也就是说字典是通过key来找值的( NSArray是通过下标找值的)
1.创建方式
a.initWithDictionary
b.[NSDictionary dictionaryWithObjects:<#(NSArray *)#> forKeys:<#(NSArray *)#>
c.NSDictionary* dictionary = @{key1 : value1 , key2 : value2 , key3 : value3 ……}<<NSArray为[]<-中括号>>
2.常用的属性
a.count
b.id obj = dictionary[key]
c.allValues, allKeysForObject
3.遍历 直接推荐forin方式
子类NSMutableDictionary
增: set***** 对键值对(一般就这个了); add***** 增加一个字典内容;
删: remove*******
改: set******(和增加是同一个方法,有的时候就修改value 没有的话就会添加一对键值对)
=========================================
比较
1.字典相对与数组的优点 (查询)
字典获取元素速度比数组快
2.获取元素的方式
数组通过下标获取指定元素的内容
字典通过key来获取指定元素的内容
3.遍历删除的问题 forin enum block 均不能在遍历的同时 删除元素 for 可以,条件设置为i--
NSDictionary类声明的对象,管理固定的键-值关联的编程接口。使用这个类或其子类nsmutabledictionary当你需要检索与任意密钥相关的数据,方便和有效的方式。NSDictionary创建静态字典,和nsmutabledictionary创建动态字典。为了方便起见,术语词典指的是这些类中的任何一个实例,而没有指定其确切的类成员。
1.2 Initializing an NSDictionary Instance(对象方法创建)
1.3 Creating Key Sets for Shared-Key Optimized Dictionaries
1.4 Counting Entries(count)
1.5 Comparing Dictionaries(比较是否相等)
1.6 Accessing Keys and Values(访问 Keys and Values)
allKeys
allKeysForObject:
allValues
objectForKey:
objectForKeyedSubscript:
objectsForKeys:notFoundMarker:
valueForKey:
1.7 Enumerating Dictionaries(枚举字典)
1.8 Sorting Dictionaries(排序字典的keys)
keysSortedByValueUsingSelector:
keysSortedByValueUsingComparator:
keysSortedByValueWithOptions:usingComparator:
1.9 Filtering Dictionaries(过滤字典)
keysOfEntriesPassingTest:
keysOfEntriesWithOptions:passingTest:
1.10 Storing Dictionaries(储存字典)
writeToFile:atomically:
writeToURL:atomically:
1.11 Accessing File Attributes(访问文件属性)
1.12 Creating a Description(创建一个描述)
1.13 Initializers(initWithCoder:)
1.14 Instance Methods
keyEnumerator
getObjects:andKeys:count:
2.2 Conforms To NSCopying NSMutableCopying NSFastEnumeration NSSecureCoding
1.1 Creating and Initializing a Mutable Dictionary(创建)
1.2 Adding Entries to a Mutable Dictionary(增)
1.3 Removing Entries From a Mutable Dictionary(删除)
1.4 Initializers
1.5 Instance Methods
1.6 Type Methods
NSDictionary
NSDictionary是无序的集合(NSArray是有序的)key->value键值对,也就是说字典是通过key来找值的( NSArray是通过下标找值的)
1.创建方式
a.initWithDictionary
b.[NSDictionary dictionaryWithObjects:<#(NSArray *)#> forKeys:<#(NSArray *)#>
c.NSDictionary* dictionary = @{key1 : value1 , key2 : value2 , key3 : value3 ……}<<NSArray为[]<-中括号>>
2.常用的属性
a.count
b.id obj = dictionary[key]
c.allValues, allKeysForObject
3.遍历 直接推荐forin方式
for (NSString *key in dictionary) { id value = dictionary[key]; NSLog(@"Value: %@ for key: %@", value, key); }
子类NSMutableDictionary
增: set***** 对键值对(一般就这个了); add***** 增加一个字典内容;
删: remove*******
改: set******(和增加是同一个方法,有的时候就修改value 没有的话就会添加一对键值对)
=========================================
比较
1.字典相对与数组的优点 (查询)
字典获取元素速度比数组快
2.获取元素的方式
数组通过下标获取指定元素的内容
字典通过key来获取指定元素的内容
3.遍历删除的问题 forin enum block 均不能在遍历的同时 删除元素 for 可以,条件设置为i--
苹果官方文档学习(部分在这 全部请至官文文档查看)
class NSDictionary
The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. Use this class or its subclass NSMutableDictionary when you need a convenient and efficient way to retrieve data associated with an arbitrary key. NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries. (For convenience, the term dictionary refers to any instance of one of these classes without specifying its exact class membership.)NSDictionary类声明的对象,管理固定的键-值关联的编程接口。使用这个类或其子类nsmutabledictionary当你需要检索与任意密钥相关的数据,方便和有效的方式。NSDictionary创建静态字典,和nsmutabledictionary创建动态字典。为了方便起见,术语词典指的是这些类中的任何一个实例,而没有指定其确切的类成员。
Symbols
1.1 Creating a Dictionary(类方法创建)1.2 Initializing an NSDictionary Instance(对象方法创建)
1.3 Creating Key Sets for Shared-Key Optimized Dictionaries
1.4 Counting Entries(count)
1.5 Comparing Dictionaries(比较是否相等)
1.6 Accessing Keys and Values(访问 Keys and Values)
allKeys
allKeysForObject:
allValues
objectForKey:
objectForKeyedSubscript:
objectsForKeys:notFoundMarker:
valueForKey:
1.7 Enumerating Dictionaries(枚举字典)
1.8 Sorting Dictionaries(排序字典的keys)
keysSortedByValueUsingSelector:
keysSortedByValueUsingComparator:
keysSortedByValueWithOptions:usingComparator:
1.9 Filtering Dictionaries(过滤字典)
keysOfEntriesPassingTest:
keysOfEntriesWithOptions:passingTest:
1.10 Storing Dictionaries(储存字典)
writeToFile:atomically:
writeToURL:atomically:
1.11 Accessing File Attributes(访问文件属性)
1.12 Creating a Description(创建一个描述)
1.13 Initializers(initWithCoder:)
1.14 Instance Methods
keyEnumerator
getObjects:andKeys:count:
Relationships
2.1 Inherits From NSObject2.2 Conforms To NSCopying NSMutableCopying NSFastEnumeration NSSecureCoding
class NSMutableDictionary
Symbols
这一部份写得有点简单 。有兴趣可以去苹果官方文档查看1.1 Creating and Initializing a Mutable Dictionary(创建)
1.2 Adding Entries to a Mutable Dictionary(增)
1.3 Removing Entries From a Mutable Dictionary(删除)
1.4 Initializers
1.5 Instance Methods
1.6 Type Methods
Relationships
2.1 Inherits From NSDictionary相关文章推荐
- Objective_C基础之NSDictionary整理
- Objective-C ---点语法 (梳理整理)
- Objective-C --- block (梳理整理)
- Objective-C ---UIButton (梳理整理)
- Objective-C ---点语法 (梳理整理)
- Objective-C ---NSFileManager NSFileHandle (梳理整理)
- Objective-C ---XML解析 (梳理整理)
- Objective-C ---NSArray(梳理整理)
- Objective-C ---UIImageView (梳理整理)
- Objective-C ---NSString(梳理整理)
- Objective-C --- block (梳理整理)
- Objective-C ---代理和协议 (梳理整理)
- Objective-C ---XML解析 (梳理整理)
- Objective-C ---NSString(梳理整理)
- Objective-C ---NSFileManager NSFileHandle (梳理整理)
- Objective-C ---NSArray NSDictionary(梳理整理)
- Objective-C ---代理和协议 (梳理整理)
- Objective-C ---UILable (梳理整理)
- 随笔-常见的数据类型常用操作整理-NSString/NSArray/NSDictionary/NSData
- Objective-C --- - UITableView 三 自定义cell(梳理总结)