How to check if UserDefaults key exists in Swift?

Posted by Wei on May 14, 2017

Question

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

Answer

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.

Reference


This is the end of post