Swift 5+
没有一个答案真正详细介绍了默认内置的本地存储功能。它可以做的不仅仅是字符串。
您可以直接从苹果文档中获得以下选项,用于从默认值中“获取”数据。
func object(forKey: String) -> Any?
//Returns the object associated with the specified key.
func url(forKey: String) -> URL?
//Returns the URL associated with the specified key.
func array(forKey: String) -> [Any]?
//Returns the array associated with the specified key.
func dictionary(forKey: String) -> [String : Any]?
//Returns the dictionary object associated with the specified key.
func string(forKey: String) -> String?
//Returns the string associated with the specified key.
func stringArray(forKey: String) -> [String]?
//Returns the array of strings associated with the specified key.
func data(forKey: String) -> Data?
//Returns the data object associated with the specified key.
func bool(forKey: String) -> Bool
//Returns the Boolean value associated with the specified key.
func integer(forKey: String) -> Int
//Returns the integer value associated with the specified key.
func float(forKey: String) -> Float
//Returns the float value associated with the specified key.
func double(forKey: String) -> Double
//Returns the double value associated with the specified key.
func dictionaryRepresentation() -> [String : Any]
//Returns a dictionary that contains a union of all key-value pairs in the domains in the search list.
这里是“设置”的选项
func set(Any?, forKey: String)
//Sets the value of the specified default key.
func set(Float, forKey: String)
//Sets the value of the specified default key to the specified float value.
func set(Double, forKey: String)
//Sets the value of the specified default key to the double value.
func set(Int, forKey: String)
//Sets the value of the specified default key to the specified integer value.
func set(Bool, forKey: String)
//Sets the value of the specified default key to the specified Boolean value.
func set(URL?, forKey: String)
//Sets the value of the specified default key to the specified URL.
如果要存储 首选项 和 不是大数据 集,这些都是非常好的选项。
双例:
设置:
let defaults = UserDefaults.standard
var someDouble:Double = 0.5
defaults.set(someDouble, forKey: "someDouble")
获取:
let defaults = UserDefaults.standard
var someDouble:Double = 0.0
someDouble = defaults.double(forKey: "someDouble")
其中一个 getter 的有趣之处在于 dictionaryRepresentation,这个方便的 getter 将获取您所有的数据类型,无论它们是什么,并将它们放入一个漂亮的字典中,您可以通过它的字符串名称和当您要求返回时,请给出正确的相应数据类型,因为它的类型是 'any'。
您也可以相应地使用func set(Any?, forKey: String) 和func object(forKey: String) -> Any? setter 和getter 来存储自己的类和对象。
希望这能进一步阐明 UserDefaults 类在存储本地数据方面的强大功能。
关于您应该存储多少以及多久存储一次,Hardy_Germany 在 post 上给出了很好的答案,这是它的引述
正如许多人已经提到的:我不知道有任何 SIZE 限制
(物理内存除外)将数据存储在 .plist 中(例如
用户默认值)。所以这不是多少钱的问题。
真正的问题应该是你多久写一次新的/改变的
值...这与此写入的电池消耗有关
原因。
IOS 没有机会避免物理写入“磁盘”,如果单个
值改变,只是为了保持数据的完整性。关于 UserDefaults
这会导致整个文件重写到磁盘。
这会为“磁盘”加电并使其保持通电更长时间,并且
防止 IOS 进入低功耗状态。
用户 Mohammad Reza Farahani 在此 post 中提到的其他需要注意的是 userDefaults 的异步和同步性质。
当您设置默认值时,它会在您的内部同步更改
进程,并与持久存储和其他进程异步。
例如,如果您保存并快速关闭程序,您可能会注意到它没有保存结果,这是因为它是异步持久化的。您可能不会一直注意到这一点,因此如果您打算在退出程序之前保存,您可能希望通过给它一些时间来完成这一点。
也许有人对此有一些不错的解决方案,可以在 cmets 中分享?