我需要创建一个数组来添加具有这种格式的对象,例如Swift中的字典:[“ key1”:“ value1”,“ key2”:“ value2”]
当我尝试NSUserDefaults
全部保存时是正确的,但是NSUserDefaults
使用键读取时会崩溃。var obj需要什么类型的数据?
let def = NSUserDefaults.standardUserDefaults()
var key = "keySave"
var element: AnyObject!
var array1: [AnyObject!] = []
array1.append(["key1": "val1", "key2": "val2"])
array1.append(["key1": "val1", "key2": "val2"])
//save
var savestring : [AnyObject!]
savestring = array1
var defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(savestring, forKey: key)
defaults.synchronize()
//read
var obj: [AnyObject!] = []
if(obj != nil){
print("size: ")
print(obj.count) //vary long value confused..
element = obj[0] //crash
print(element.objectForKey("key1"))
}