How to save a Array with (Multiple Types) in NSUserDefaults

前端 未结 2 456
长情又很酷
长情又很酷 2021-01-23 06:33

This is pretty simple but can\'t seem to find the correct information to solve saving an array like this in User Defaults.

It says it\'s not a property that NSUser Defau

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 07:00

    You have to store your Object in form of Data Convert into data using NSKeyedArchiver.archivedData(withRootObject:)

    Convert back to Object using NSKeyedUnarchiver.unarchiveObject(with:)

    Saving Data for UserDefaults

    let notificationData = NSKeyedArchiver.archivedData(withRootObject: notificationList)
    UserDefaults.standard.set(notificationData, forKey: "notificationList")
    

    Retrive Data from User UserDefaults

    let decodedData  = UserDefaults.standard.object(forKey: "notificationList") as! Data
    let notificationList = NSKeyedUnarchiver.unarchiveObject(with: decodedData) as! AnyObject
    

提交回复
热议问题