Reputation: 31645
As a convention, when dealing with UserDefaults
, we use the standard
object of it. However, using the standard UserDefaults
should set/get values that are accessible from a new instance and vise versa. To clarify:
let object = UserDefaults()
object.set(101, forKey: "number1") // setting via a new object
UserDefaults.standard.integer(forKey: "number1") // getting via standard
// 101
UserDefaults.standard.set(102, forKey: "number2") // setting via standard
object.value(forKey: "number2") // getting via the new object
// 102
I would assume that standard
is not exists as "syntactic sugar", it doesn't seem to be sensible to create a singleton class just for this purpose.
So:
standard
instead of UserDefaults()
? If there is, what is it? Could it be related to the suites?UserDefaults()
?Upvotes: 0
Views: 216
Reputation: 385580
Calling UserDefaults()
is the same as calling UserDefaults(suiteName: nil)
.
Calling UserDefaults(suiteName: nil)
returns an object that behaves like UserDefaults.standard
:
If you pass
nil
to this parameter, the system uses the default search list that thestandard
class method uses.
So there is no advantage in using UserDefaults()
instead of UserDefaults.standard
.
The advantage of using UserDefaults.standard
instead of UserDefaults()
is that only a single UserDefaults.standard
object exists, so it may be faster and may use less memory to use UserDefaults.standard
than to create a new object by calling UserDefaults()
.
Upvotes: 5