check if key exist in NSUserDefaults

4k Views Asked by At

I'm trying to check whether a specific key in NSUserDefault exists. The problem is that it keep returning error. How come it returns lldb error?

if let data = NSUserDefaults.standardUserDefaults().objectForKey("teams") as? NSData {

}
1

There are 1 best solutions below

3
On

This code works for me. Try it :

if (NSUserDefaults.standardUserDefaults().objectForKey("yourKey") != nil) {
     // perform your task here
}