Adil Hussain
Adil Hussain

Reputation: 32161

Why is UserDefaults.init(suiteName:) a failable initializer?

The UserDefaults.init(suiteName:) initializer is a failable initializer. However, the documentation does not specify the circumstances under which it will return a nil UserDefaults object.

Firstly, what are the circumstances under which it will return a nil UserDefaults object?

Secondly, am I safe to force-unwrap it in the context of an iPhone and iPad application?

Upvotes: 4

Views: 1151

Answers (1)

Adil Hussain
Adil Hussain

Reputation: 32161

The UserDefaults.init(suiteName:) initializer will return a nil UserDefaults object if it is passed the app's main bundle identifier or globalDomain. If you're passing in any other value, it is my guess that you're okay to assume that this initializer will return a non-nil UserDefaults object.

(Thank you to @Larme for his/her comment under my question.)

Upvotes: 3

Related Questions