LoginSignup
1
4

More than 5 years have passed since last update.

[Swift]UserDefaultsが設定されているか確認する方法

Posted at

UserDefaultsbool(forKey:)integer(forKey:)などはnullableでない値を返してくるため、設定されていなくてもfalse0が返って来てしまう。
そういった場合に値が設定されているかを確認する場合は、object(forKey:)を調べると良い。

let isEnabled: Bool?
if UserDefaults.standard.object(forKey: "isEnabled") == nil {
    print("isEnabled is not set")
} else {
    isEnabled = defaults.bool(forKey: "isEnabled")
}
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4