Reputation: 83
I want to clear userdefault but I could not understand difference between
UserDefaults.standard.set(nil, forKey: "token") // when should i use this
UserDefaults.standard.removeObject(forKey: "token") // when should i use this
Thanks
Upvotes: 2
Views: 601
Reputation: 87894
No difference at all.
If you want to remove an object, use second variant for better readability
First one useful when you have an optional value and don't want care if it's nil
, like
var a: String? = "hello"
UserDefaults.standard.set(a, forKey: "token")
b = nil
UserDefaults.standard.set(a, forKey: "token")
Otherwise you would have to check value and choose to either insert or delete
Upvotes: 3