问题
Is there a difference between UserDefaults()
and UserDefaults.standard
in Swift 3.0?
回答1:
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")
来源:https://stackoverflow.com/questions/40026196/difference-between-userdefaults-and-userdefaults-standard