我需要创建一个数组来添加这样的格式,如Swift中的字典:[“key1”:“value1”,“key2”:“value2”]
@H_301_6@当我尝试使用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")) }