-
UserDefaults
,示例:
struct defaultsKeys {
static let key1 = "key1"
static let key2 = "key2"
static let key3 = "key3"
}
let defaults = UserDefaults.standard
// 写入
defaults.setValue("Some String Value", forKey: defaultsKeys.key1)
defaults.setValue(100.0, forKey: defaultsKeys.key2)
defaults.setValue(true, forKey: defaultsKeys.key3)
// 读取
if let value1 = defaults.string(forKey: defaultsKeys.key1) {
print(value1)
}
let value2 = defaults.float(forKey: defaultsKeys.key2)
print(value2)
let value3 = defaults.bool(forKey: defaultsKeys.key3)
print(value3)
官方文档:
https://developer.apple.com/documentation/foundation/userdefaults
-
Keychain
钥匙串
适用于存储安全性要求很高的数据,不适合存储比较大数据 -
CoreData
(创建项目时,选择Use Core Data
) -
FileManager
可用于iCloud存储,能在多设备中同步
读写时相对比较慢 -
plist
(Property Lists)
网友评论