无法使用NSCoder解码自定义类

时间:2017-02-12 00:52:44

标签: ios swift xcode persistence nscoding

我的用户Class符合NSCoder,并且具有符合Classes的自定义NSCoder属性。当应用程序关闭时,它成功(据我所知)encodes,如下所示 AppDelegate类:

    func applicationWillTerminate(_ application: UIApplication) {
        saveuser()
    }

saveuser():

func saveuser() {
    let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(globUs, toFile: User.archiveURL.path)
    if isSuccessfulSave {
        print("user saved")
    } else {
        print("failed save")
    }
}

所有这些都很好,但是当我再次启动应用程序时,我会在User Class中使用此功能。

required convenience init?(coder aDecoder: NSCoder) {
    guard let firstNam = aDecoder.decodeObject(forKey: coderKey.fName) as? String else {
        print("trouble decoding first name")
        return nil
    }
    guard let lastNam = aDecoder.decodeObject(forKey: coderKey.lName) as? String else {
        print("trouble decoding last name")
        return nil
    }
    guard let bi = aDecoder.decodeObject(forKey: coderKey.bio) as? String else {
        print("trouble decoding bio")
        return nil
    }
    guard let tag = aDecoder.decodeObject(forKey: coderKey.tags) as? [Tag] else {
        print("trouble decoding tags")
        return nil
    }
    guard let organization = aDecoder.decodeObject(forKey: coderKey.orgs) as? [Organization] else {
        print("trouble decoding orgs")
        return nil
    }
    guard let im = aDecoder.decodeObject(forKey: coderKey.orgs) as? UIImage else {
        print("trouble decoding img")
        return nil
    }
    self.init(n: firstNam, l: lastNam, b: bi, t: tag, o: organization, i: im)
}

打印

  

解码名字

并退出。这意味着aDecoder.decodeObject无效。关于我做错了什么想法?

0 个答案:

没有答案