NSUserDefaults
用于持久化存储少量用户/插件配置。通过 NSUserDefaults.standardUserDefaults() 获取标准实例。适合保存开关、计数、字符串等简单类型。
类成员 (Class members)
Section titled “类成员 (Class members)”standardUserDefaults
Section titled “standardUserDefaults”获取标准用户默认设置实例。
static standardUserDefaults(): NSUserDefaultsReturn Value:
NSUserDefaults: 单例对象。
实例成员 (Instance members)
Section titled “实例成员 (Instance members)”setObjectForKey
Section titled “setObjectForKey”存储任意对象(通常为字符串、数字、数组、字典等可序列化类型)。
setObjectForKey(value: any, key: string): voidParameters:
| Name | Type | Description |
|---|---|---|
value | any | 要存储的值。 |
key | string | 键名。 |
setIntegerForKey
Section titled “setIntegerForKey”存储整数。
setIntegerForKey(value: number, key: string): voidParameters:
| Name | Type | Description |
|---|---|---|
value | number | 整数值。 |
key | string | 键名。 |
setFloatForKey
Section titled “setFloatForKey”存储浮点数。
setFloatForKey(value: number, key: string): voidParameters:
| Name | Type | Description |
|---|---|---|
value | number | 浮点数值。 |
key | string | 键名。 |
setDoubleForKey
Section titled “setDoubleForKey”存储双精度浮点数。
setDoubleForKey(value: number, key: string): voidParameters:
| Name | Type | Description |
|---|---|---|
value | number | 双精度数值。 |
key | string | 键名。 |
setBoolForKey
Section titled “setBoolForKey”存储布尔值。
setBoolForKey(value: boolean, key: string): voidParameters:
| Name | Type | Description |
|---|---|---|
value | boolean | 布尔值。 |
key | string | 键名。 |
setURLForKey
Section titled “setURLForKey”存储 URL。
setURLForKey(url: NSURL, key: string): voidParameters:
| Name | Type | Description |
|---|---|---|
url | NSURL | URL 对象。 |
key | string | 键名。 |
objectForKey
Section titled “objectForKey”读取任意对象。
objectForKey(key: string): anyParameters:
| Name | Type | Description |
|---|---|---|
key | string | 键名。 |
Return Value:
any: 存储的对象(若不存在则返回 undefined/null)。
stringForKey
Section titled “stringForKey”读取字符串。
stringForKey(key: string): string | nullReturn Value:
string | null: 字符串值。
arrayForKey
Section titled “arrayForKey”读取数组。
arrayForKey(key: string): NSArray | nullReturn Value:
NSArray | null: 数组对象。
dictionaryForKey
Section titled “dictionaryForKey”读取字典。
dictionaryForKey(key: string): NSDictionary | nullReturn Value:
NSDictionary | null: 字典对象。
dataForKey
Section titled “dataForKey”读取二进制数据。
dataForKey(key: string): NSData | nullReturn Value:
NSData | null: 数据对象。
stringArrayForKey
Section titled “stringArrayForKey”读取字符串数组。
stringArrayForKey(key: string): string[] | nullReturn Value:
string[] | null: 字符串数组。
integerForKey
Section titled “integerForKey”读取整数。
integerForKey(key: string): numberReturn Value:
number: 整数值(若不存在通常返回 0)。
floatForKey
Section titled “floatForKey”读取浮点数。
floatForKey(key: string): numberReturn Value:
number: 浮点数值。
doubleForKey
Section titled “doubleForKey”读取双精度浮点数。
doubleForKey(key: string): numberReturn Value:
number: 双精度数值。
boolForKey
Section titled “boolForKey”读取布尔值。
boolForKey(key: string): booleanReturn Value:
boolean: 布尔值(若不存在通常返回 false)。
URLForKey
Section titled “URLForKey”读取 URL。
URLForKey(key: string): NSURL | nullReturn Value:
NSURL | null: URL 对象。
removeObjectForKey
Section titled “removeObjectForKey”移除指定键的值。
removeObjectForKey(key: string): voidParameters:
| Name | Type | Description |
|---|---|---|
key | string | 键名。 |
synchronize
Section titled “synchronize”强制立即写入磁盘。
synchronize(): booleanReturn Value:
boolean: 是否写入成功。
Note: 系统通常会自动保存,仅在需要立即持久化时调用。
registerDefaults
Section titled “registerDefaults”注册默认值(在未设置值时生效)。
registerDefaults(registrationDictionary: NSDictionary): voidParameters:
| Name | Type | Description |
|---|---|---|
registrationDictionary | NSDictionary | 包含默认键值对的字典。 |