Swift 3 UserDefaults设置了NSMutableArray

时间:2017-11-25 09:56:05

标签: swift swift3 nsmutablearray nsuserdefaults userdefaults

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()

1 个答案:

答案 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!)