Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 859
Adil Hussain Avatar asked Sep 19 '25 09:09

Adil Hussain


1 Answers

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.)

like image 73
Adil Hussain Avatar answered Sep 23 '25 11:09

Adil Hussain