UserDefaults()和UserDefaults.standard之间的区别 [英] Difference between UserDefaults() and UserDefaults.standard
问题描述
Swift 3.0中的UserDefaults()
和UserDefaults.standard
之间是否有区别?
UserDefaults -为您提供new object
,为每个对象分配了不同的内存,并在对象作用域完成时释放. >
UserDefaults.standard -使用类方法standard
为您提供singleton
对象,该方法接收的对象在整个应用程序中分配了单个内存.
如果您对此有兴趣,还可以使用它们:
// Set
UserDefaults.standard.set("YOUR STRING", forKey: "key")
UserDefaults().set("YOUR STRING", forKey: "key")
// Get
UserDefaults.standard.string(forKey: "key")
UserDefaults().string(forKey: "key")
Is there a difference between UserDefaults()
and UserDefaults.standard
in Swift 3.0?
UserDefaults - Gives you a new object
, each object is allocated a different memory and deallocated when object scope is finished.
UserDefaults.standard - Gives you the singleton
object by using the class method standard
the object received by this method is allocated single memory throughout the application.
And the usage of them if you´re interesedted in that:
// Set
UserDefaults.standard.set("YOUR STRING", forKey: "key")
UserDefaults().set("YOUR STRING", forKey: "key")
// Get
UserDefaults.standard.string(forKey: "key")
UserDefaults().string(forKey: "key")
这篇关于UserDefaults()和UserDefaults.standard之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!