有没有一种方法可以使用unarchiveTopLevelObjectWithData()

时间:2018-12-30 04:13:33

标签: ios swift

我有一个在取消存档过程中创建对象(两个字符串和一张NSData图片)的类,我得到了一个指针。我假设这是我的对象驻留在内存中的位置。这是从内存中取消存档此已保存对象的最合适方法。

这个项目我想制作一个应用程序,通过使用API​​,您可以查看食谱。我正在尝试使用户可以上传自己的食谱的另一个功能。因为这是我的第一个应用程序,所以我试图了解数据持久性。

除了我需要从UserDefaults中读取未存档数据的部分之外,该代码似乎正常工作。

class myRecipes: NSObject, NSCoding {
    var publisher: String
    var image_url: NSData
    var title: String

    enum SerializationError:Error {
        case missing(String)
        case invalid(String, Any)
    }

    init(title: String, publisher: String, image_url: NSData) throws {
        self.publisher = publisher
        self.image_url = image_url
        self.title = title
    }

    required convenience init(coder aDecoder: NSCoder) {
        let publisher = aDecoder.decodeObject(forKey: "publisher") as! String
        let image_url = aDecoder.decodeObject(forKey: "image_url") as! NSData
        let title = aDecoder.decodeObject(forKey: "title") as! String
        try! self.init(title: title, publisher: publisher, image_url: image_url)

    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(publisher, forKey: "publisher")
        aCoder.encode(image_url, forKey: "image_url")
        aCoder.encode(title, forKey: "title")
    }  
}


func applicationDidEnterBackground(_ application: UIApplication) {

        let userDefaults = UserDefaults.standard
        let encodedData: Data = try! NSKeyedArchiver.archivedData(withRootObject: myRecipess, requiringSecureCoding: false)
        userDefaults.set(encodedData, forKey: "myRecipess")
        userDefaults.synchronize()
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // set the notification badge to 0.
        application.applicationIconBadgeNumber = 0

        // decode the use's recipes.
        let userDefaults = UserDefaults.standard
        let decoded  = userDefaults.object(forKey: "myRecipess") as! Data
        let decodedTeams = try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(decoded) as! [myRecipes]
        print("decodedTeams")
        print(decodedTeams)
    }

我希望从内存中检索数据。

1 个答案:

答案 0 :(得分:0)

在Swift 4中,强烈建议使用Codable协议来存档数据。

首先使用struct并以单数形式命名

struct Recipe : Codable {
    var publisher: String
    var imageUrl: Data
    var title: String
}

然后您有一个数据源数组

var recipes = [Recipe]()

要保存数据写

func applicationDidEnterBackground(_ application: UIApplication) {

    do {
        let encodedData = try PropertyListEncoder().encode(recipes)
        UserDefaults.standard.set(encodedData, forKey: "recipes")
    } catch { print(error) }
}

要加载数据写入

func applicationDidBecomeActive(_ application: UIApplication) {
    // set the notification badge to 0.
    application.applicationIconBadgeNumber = 0

    // decode the use's recipes.
    if let decoded  = UserDefaults.standard.data(forKey: "recipes") {
       do {
          recipes = try PropertyListDecoder().decode([Recipe].self, from: decoded)
          return
       } catch { 
         print(error) 
       }
    }
    recipes = []
}
相关问题