Saving NSMutableArray with NSUserDefaults in Swift Returns Nil
setObject
方法删除了我的代码并且无法正常工作。我尝试所有设置类型仍然无法正常工作。任何想法?
var banksAccount : NSMutableArray = []
for bankJson in (self.users?[0].Banks)! {
self.banksAccount.add(bankJson)
}
UserDefaults.standard.set(self.banksAccount, forKey: "myBanksAccounts")
UserDefaults.standard.synchronize()
答案 0 :(得分:0)
保存
let cacheArrayData = NSKeyedArchiver.archivedData(withRootObject: banksAccount)
UserDefaults.standard.set(cacheArrayData, forKey: "EventItems")
用于检索
let retriveArrayData = UserDefaults.standard.object(forKey: "EventItems") as? NSData
let retriveArray = NSKeyedUnarchiver.unarchiveObject(with: retriveArrayData! as Data) as? NSArray
self.eventsArray = NSMutableArray(array:retriveArray!)