NSDictionary
NSDictionary 是不可变的键值集合类型。常见于 API 返回 NSDictionary(例如 options、headerFields、配置对象等)。
类成员 (Class members)
Section titled “类成员 (Class members)”dictionary
Section titled “dictionary”返回空字典。
static dictionary(): NSDictionaryReturn Value:
NSDictionary: 空字典实例。
dictionaryWithObjectForKey
Section titled “dictionaryWithObjectForKey”从单键值对创建字典。
static dictionaryWithObjectForKey(object: any, key: any): NSDictionaryParameters:
| Name | Type | Description |
|---|---|---|
object | any | 值。 |
key | any | 键(需支持 NSCopying)。 |
Return Value:
NSDictionary: 新字典。
dictionaryWithDictionary
Section titled “dictionaryWithDictionary”从已有字典拷贝创建。
static dictionaryWithDictionary(dict: NSDictionary): NSDictionaryParameters:
| Name | Type | Description |
|---|---|---|
dict | NSDictionary | 源字典。 |
Return Value:
NSDictionary: 新字典。
dictionaryWithObjectsForKeys
Section titled “dictionaryWithObjectsForKeys”从键数组与值数组创建。
static dictionaryWithObjectsForKeys(objects: NSArray, keys: NSArray): NSDictionaryParameters:
| Name | Type | Description |
|---|---|---|
objects | NSArray | 值数组。 |
keys | NSArray | 键数组。 |
Return Value:
NSDictionary: 新字典。
dictionaryWithObjectsAndKeys
Section titled “dictionaryWithObjectsAndKeys”从可变参数键值对创建(firstObject, firstKey, …)。
static dictionaryWithObjectsAndKeys(firstObject: any, firstKey: any, ...args: any[]): NSDictionaryParameters:
| Name | Type | Description |
|---|---|---|
firstObject | any | 第一个值。 |
firstKey | any | 第一个键。 |
...args | any[] | 其余键值交替。 |
Return Value:
NSDictionary: 新字典。
dictionaryWithContentsOfFile
Section titled “dictionaryWithContentsOfFile”从 plist 文件路径创建。
static dictionaryWithContentsOfFile(path: string): anyParameters:
| Name | Type | Description |
|---|---|---|
path | string | 文件路径。 |
Return Value:
any: 解析得到的字典或 null。
dictionaryWithContentsOfURL
Section titled “dictionaryWithContentsOfURL”从 URL 创建。
static dictionaryWithContentsOfURL(url: NSURL): anyParameters:
| Name | Type | Description |
|---|---|---|
url | NSURL | 资源 URL。 |
Return Value:
any: 解析得到的字典或 null。
实例成员 (Instance members)
Section titled “实例成员 (Instance members)”键值对个数。
count(): numberReturn Value:
number: 字典大小。
allKeys
Section titled “allKeys”所有键组成的数组。
allKeys(): NSArrayReturn Value:
NSArray: 键数组。
allValues
Section titled “allValues”所有值组成的数组。
allValues(): NSArrayReturn Value:
NSArray: 值数组。
objectForKey
Section titled “objectForKey”按键取值。
objectForKey(key: any): anyParameters:
| Name | Type | Description |
|---|---|---|
key | any | 键。 |
Return Value:
any: 对应值或 undefined。
objectForKeyedSubscript
Section titled “objectForKeyedSubscript”下标访问(同 objectForKey)。
objectForKeyedSubscript(key: any): anyParameters:
| Name | Type | Description |
|---|---|---|
key | any | 键。 |
Return Value:
any: 对应值或 undefined。
keysSortedByValueUsingSelector
Section titled “keysSortedByValueUsingSelector”按键对应值的排序结果返回键数组。
keysSortedByValueUsingSelector(comparator: any): NSArrayParameters:
| Name | Type | Description |
|---|---|---|
comparator | any | 比较方法。 |
Return Value:
NSArray: 排序后的键数组。
isEqualToDictionary
Section titled “isEqualToDictionary”是否与另一字典内容相等。
isEqualToDictionary(otherDictionary: NSDictionary): booleanParameters:
| Name | Type | Description |
|---|---|---|
otherDictionary | NSDictionary | 另一字典。 |
Return Value:
boolean: 是否相等。
description
Section titled “description”返回字典的字符串描述。
description(): stringReturn Value:
string: 描述字符串。
descriptionWithLocale
Section titled “descriptionWithLocale”按 locale 返回描述。
descriptionWithLocale(locale: any): stringParameters:
| Name | Type | Description |
|---|---|---|
locale | any | locale。 |
Return Value:
string: 描述字符串。
descriptionWithLocaleIndent
Section titled “descriptionWithLocaleIndent”按 locale 与缩进返回描述。
descriptionWithLocaleIndent(locale: any, level: number): stringParameters:
| Name | Type | Description |
|---|---|---|
locale | any | locale。 |
level | number | 缩进层级。 |
Return Value:
string: 描述字符串。
keyEnumerator
Section titled “keyEnumerator”键的枚举器。
keyEnumerator(): NSEnumeratorReturn Value:
NSEnumerator: 键枚举器。
objectEnumerator
Section titled “objectEnumerator”值的枚举器。
objectEnumerator(): NSEnumeratorReturn Value:
NSEnumerator: 值枚举器。
enumerateKeysAndObjectsUsingBlock
Section titled “enumerateKeysAndObjectsUsingBlock”用 block 遍历键值对。
enumerateKeysAndObjectsUsingBlock(block: (key: any, obj: any, stop: any) => void): voidParameters:
| Name | Type | Description |
|---|---|---|
block | (key, obj, stop) => void | 每对调用的回调。 |
Return Value:
- 无。
writeToFileAtomically
Section titled “writeToFileAtomically”写入 plist 文件。
writeToFileAtomically(path: string, useAuxiliaryFile: boolean): booleanParameters:
| Name | Type | Description |
|---|---|---|
path | string | 文件路径。 |
useAuxiliaryFile | boolean | 是否先写临时再替换。 |
Return Value:
boolean: 是否成功。
writeToURLAtomically
Section titled “writeToURLAtomically”写入 URL。
writeToURLAtomically(url: NSURL, atomically: boolean): booleanParameters:
| Name | Type | Description |
|---|---|---|
url | NSURL | 目标 URL。 |
atomically | boolean | 是否原子写入。 |
Return Value:
boolean: 是否成功。
NSMutableDictionary— 可变字典- NSArray