How can I save [AnyObject!] = [] In NSUserDefaults in Swift?
I need to store a variable like:
var array1: [AnyObject!] = []
I tried this but didn't save:
var key = "keySave"
var defaults = NSUserDefaults.standardUserDefaults()
array1.append(["key1": "val1", "key2": "val2"])
array1.append(["key2": "val3", "key4": "val4"])
defaults.setObject(array1, forKey: key)
defaults.synchronize()
Do I need to pass these variables to other data types? What's the correct shape for this?
Thank!
source to share
It is forbidden. According to the class documentationNSUserDefaults
, only these specific types are supported by the method setObject:forKey:
:
The parameter value can only be the subject of a property list:
NSData
,NSString
,NSNumber
,NSDate
,NSArray
orNSDictionary
. For objectsNSArray
andNSDictionary
their contents must be property list objects.
, Swift NSMutableArray
, NSArray
.