How to check if UserDefaults key exists in Swift?

Posted by Wei Xia on May 14, 2017 | Hits:


I wanna check if any specific UserDefaults key exists in the application, how to start with it?


The answer below is using Swift 3.

func isKeyPresentInUserDefaults(key: String) -> Bool {
    return UserDefaults.standard.object(forKey: key) != nil

When you use this function, simply create a variable by using

let checkIfCookieExist = userAlreadyExist(KeyName: defaultsKeys.KeyName)

it will return a boolean.


This is the end of post