user2924482 user2924482 - 5 months ago 24
iOS Question

Swift: Is not saving NSUserDefaults.standardUserDefaults values

I'm trying to save key value in the NSUserDefaults but is not been save. Here is my code:

func saveData() {

let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject("blablabla", forKey:"data")
userDefaults.synchronize()
}


Any of you knows why this data is not been save?

I'll really appreciate your help.

Answer

According with Apple sources:

public func valueForKey(key: String) -> AnyObject?

to retrieve your value you could use:

if let value = userDefaults.valueForKey("data")  {
    // do whatever you want with your value
    // P.S. value could be numeric,string,..
}