使用 Codable 进行归档 Swift



我正在尝试使用可编码的swift在iOS应用程序中保存文本字段中的一些数据,但是当我定义用于存档xcode的函数时,告诉我"无法推断通用参数'值'"。

正在学习归档,如果有人能帮助我,我会很棒。

这是我正在使用的代码

class ProfileViewController: UITableViewController {
    var currentProfile: Profile?
    @IBOutlet weak var name: UITextField!
    @IBOutlet weak var surname: UITextField!
    @IBAction func saveProfileButton(_ sender: Any) {
        addCurrentProfile()
        saveProfile()
    }
    func addCurrentProfile(){
        currentProfile?.name = name.text ?? ""
        currentProfile?.surname = surname.text ?? ""
    }
    struct Profile {
        var name: String
        var surname: String
        enum CodingKeys: String, CodingKey {
            case name
            case surname
        }
        init(name:String, surname:String) {
            self.name = name
            self.surname = surname
        }
        func encode(to encoder: Encoder) throws {
            var container = encoder.container(keyedBy: CodingKeys.self)
            try container.encode(name, forKey: .name)
            try container.encode(surname, forKey: .surname)
        }
        init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: CodingKeys.self)
            name = try container.decode(String.self, forKey: .name)
            surname = try container.decode(String.self, forKey: .surname)
        }
    }
    func saveProfile() {
        do {
            let data = try PropertyListEncoder().encode(currentProfile)
            let success = NSKeyedArchiver.archiveRootObject(data, toFile: profile_data.path)
            print(success ? "Successful save" : "Save Failed")
        } catch {
            print("Save Failed")
        }
    }
}

第一件事是让你的结构确认Codable协议,如下所示:

struct Profile:Codable {
        var name: String
        var surname: String
        enum CodingKeys: String, CodingKey {
            case name
            case surname
        }
        init(name:String, surname:String) {
            self.name = name
            self.surname = surname
        }
        func encode(to encoder: Encoder) throws {
            var container = encoder.container(keyedBy: CodingKeys.self)
            try container.encode(name, forKey: .name)
            try container.encode(surname, forKey: .surname)
        }
        init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: CodingKeys.self)
            name = try container.decode(String.self, forKey: .name)
            surname = try container.decode(String.self, forKey: .surname)
        }
    }

然后,您可以使用PropertyListEncoder实现该对象,如下所示:

func saveProfile() {
        do {
            let data = try PropertyListEncoder().encode(currentProfile)
            let success = NSKeyedArchiver.archiveRootObject(data, toFile: profile_data.path)
            print(success ? "Successful save" : "Save Failed")
        } catch {
            print("Save Failed")
        }
    }

这里似乎有几个问题。直接回答您的问题:"无法推断通用参数'值'"。是:您忘记添加协议:

 struct Profile: Codable {
...}

相关内容

  • 没有找到相关文章

最新更新