簡體   English   中英

Swift 3 NSCoder無法識別的選擇器發送到實例

[英]Swift 3 NSCoder unrecognized selector sent to instance

我試圖將自定義類的列表保存為用戶默認值,並且在保存時始終收到“無法識別的選擇器發送給實例”錯誤。 從用戶默認設置獲取陣列時,我沒有任何問題。

這是我用來保存為用戶默認值的代碼。

let userDefaults = UserDefaults.standard
        userDefaults.set(NSKeyedArchiver.archivedData(withRootObject: visitors), forKey: "signedInUsers")
        userDefaults.synchronize()

Visitor是一個Visitor對象數組。

這是我的訪客班。

class Visitor: NSObject {
    var name: String = ""
    var contactEmail: String = ""
    var company: String = ""
    var picture: UIImage = UIImage()
    var signature: UIImage = UIImage()
    var timeIn: String = ""
    var timeOut: String = ""
    var signedOut: Bool = false
    var otherQuestionsWithAnswers: [String] = []

    // MARK: NSCoding

    override init() {
        super.init()
    }

    init(name: String, contactEmail: String, company: String, picture: UIImage, signature: UIImage,timeIn: String,timeOut: String,signedOut: Bool,otherQuestionsWithAnswers: [String]) {
        self.name = name
        self.contactEmail = contactEmail
        self.company = company
        self.picture = picture
        self.signature = signature
        self.timeIn = timeIn
        self.timeOut = timeOut
        self.signedOut = signedOut
        self.otherQuestionsWithAnswers = otherQuestionsWithAnswers
    }

    required convenience init?(coder decoder: NSCoder) {
        guard let name = decoder.decodeObject(forKey: "name") as? String,
            let contactEmail = decoder.decodeObject(forKey: "contactEmail") as? String,
            let company = decoder.decodeObject(forKey: "company") as? String,
            let timeOut = decoder.decodeObject(forKey: "timeOut") as? String,
            let timeIn = decoder.decodeObject(forKey: "timeIn") as? String,
            let picture = decoder.decodeObject(forKey: "picture") as? UIImage,
            let signature = decoder.decodeObject(forKey: "signature") as? UIImage,
            let otherQuestionsWithAnswers = decoder.decodeObject(forKey: "otherQuestionsWithAnswers") as? [String],
            let signedOut = decoder.decodeObject(forKey: "signedOut") as? Bool
            else { return nil }

        self.init(
            name: name,
            contactEmail: contactEmail,
            company: company,
            picture: picture,
            signature: signature,
            timeIn: timeIn,
            timeOut: timeOut,
            signedOut: signedOut,
            otherQuestionsWithAnswers: otherQuestionsWithAnswers
        )
    }

    func encodeWithCoder(coder: NSCoder) {
        coder.encode(name, forKey: "name")
        coder.encode(contactEmail, forKey: "contactEmail")
        coder.encode(company, forKey: "company")
        coder.encode(picture, forKey: "picture")
        coder.encode(signature, forKey: "signature")
        coder.encode(timeIn, forKey: "timeIn")
        coder.encode(timeOut, forKey: "timeOut")
        coder.encode(signedOut, forKey: "signedOut")
        coder.encode(otherQuestionsWithAnswers, forKey: "otherQuestionsWithAnswers")
    }
}

感謝您的幫助。

編輯:

將類更改為此固定。

import Foundation
import UIKit

// a question that the user has to answer
class Visitor: NSObject, NSCoding {
    var name: String = ""
    var contactEmail: String = ""
    var company: String = ""
    var picture: UIImage = UIImage()
    var signature: UIImage = UIImage()
    var timeIn: String = ""
    var timeOut: String = ""
    var signedOut: Bool = false
    var otherQuestionsWithAnswers: [String] = []

    // MARK: NSCoding

    override init() {
        super.init()
    }

    init(name: String, contactEmail: String, company: String, picture: UIImage, signature: UIImage,timeIn: String,timeOut: String,signedOut: Bool,otherQuestionsWithAnswers: [String]) {
        self.name = name
        self.contactEmail = contactEmail
        self.company = company
        self.picture = picture
        self.signature = signature
        self.timeIn = timeIn
        self.timeOut = timeOut
        self.signedOut = signedOut
        self.otherQuestionsWithAnswers = otherQuestionsWithAnswers
    }

    required init(coder decoder: NSCoder) {
        name = decoder.decodeObject(forKey: "name") as! String
        contactEmail = decoder.decodeObject(forKey: "contactEmail") as! String
        company = decoder.decodeObject(forKey: "company") as! String
        timeOut = decoder.decodeObject(forKey: "timeOut") as! String
        timeIn = decoder.decodeObject(forKey: "timeIn") as! String
        if let pictureTest = decoder.decodeObject(forKey: "picture") as? UIImage {
            picture = pictureTest
        } else {
            picture = UIImage()
        }
        if let sigTest = decoder.decodeObject(forKey: "picture") as? UIImage {
            signature = sigTest
        } else {
            signature = UIImage()
        }
        otherQuestionsWithAnswers = decoder.decodeObject(forKey: "otherQuestionsWithAnswers") as! [String]
        signedOut = decoder.decodeBool(forKey: "signedOut")
    }

    public func encode(with aCoder: NSCoder) {
        aCoder.encode(name, forKey: "name")
        aCoder.encode(contactEmail, forKey: "contactEmail")
        aCoder.encode(company, forKey: "company")
        aCoder.encode(picture, forKey: "picture")
        aCoder.encode(signature, forKey: "signature")
        aCoder.encode(timeIn, forKey: "timeIn")
        aCoder.encode(timeOut, forKey: "timeOut")
        aCoder.encode(signedOut, forKey: "signedOut")
        aCoder.encode(otherQuestionsWithAnswers, forKey: "otherQuestionsWithAnswers")
    }
}

您正在使用的編碼功能不正確,必須為func encode(with aCoder: NSCoder){}而不是func encodeWithCoder(coder: NSCoder) {}因此請進行更改並按預期工作

public func encode(with aCoder: NSCoder) {
    aCoder.encode(name, forKey: "name")
    aCoder.encode(contactEmail, forKey: "contactEmail")
    aCoder.encode(company, forKey: "company")
    aCoder.encode(picture, forKey: "picture")
    aCoder.encode(signature, forKey: "signature")
    aCoder.encode(timeIn, forKey: "timeIn")
    aCoder.encode(timeOut, forKey: "timeOut")
    aCoder.encode(signedOut, forKey: "signedOut")
    aCoder.encode(otherQuestionsWithAnswers, forKey: "otherQuestionsWithAnswers")
}

希望這可以幫助

您的Visitor類應首先從NSObject繼承,再從NSCoding繼承。 沒有NSCoding,就無法對自定義類進行編碼和解碼。 這是我最近在我的項目中犯的一個錯誤,並且您的編碼函數簽名不正確。 我敢肯定,這將解決此問題。 這是更正的代碼:

Swift 3版本:-

class Visitor: NSObject, NSCoding {
    var name: String = ""
    var contactEmail: String = ""
    var company: String = ""
    var picture: UIImage = UIImage()
    var signature: UIImage = UIImage()
    var timeIn: String = ""
    var timeOut: String = ""
    var signedOut: Bool = false
    var otherQuestionsWithAnswers: [String] = []

func encode(with aCoder: NSCoder) {
//... encode here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM