encodeWithCoder无法识别的选择器发送到实例

时间:2017-07-12 14:33:23

标签: ios swift cocoa-touch nsdata nscoding

我得到了这个例外,但我无法弄清楚我哪里出错了。我正在编写一个扩展,试图将此结构从NewsAPISwiftLibrary保存到NSUserDefaults:NewsAPISource

这是我的扩展程序的代码:

extension NewsAPISource {

    init?(data: NSData) {
        if let coding = NSKeyedUnarchiver.unarchiveObject(with: data as Data) as? Encoding {
            id = coding.id
            name = coding.name
            sourceDescription = coding.sourceDescription
            url = coding.url
            category = coding.category
            language = coding.language
            country = coding.country
            sortBysAvailable = coding.sortBysAvailable as [SortBy]
        } else {
            return nil
        }
    }

    func encode() -> NSData {
        return NSKeyedArchiver.archivedData(withRootObject: Encoding(self)) as NSData
    }

    private class Encoding: NSObject, NSCoding {

        let id: SourceId?
        let name: String?
        let sourceDescription: String?
        let url: String?
        let category: NewsAPISwift.Category?
        let language: Language?
        let country: Country?
        let sortBysAvailable: [SortBy]

        init(_ source: NewsAPISource) {
            id = source.id
            name = source.name
            sourceDescription = source.sourceDescription
            url = source.url
            category = source.category
            language = source.language
            country = source.country
            sortBysAvailable = source.sortBysAvailable
        }

        required init?(coder aDecoder: NSCoder) {
            id = aDecoder.decodeObject(forKey: "id") as? SourceId
            name = aDecoder.decodeObject(forKey: "name") as? String
            sourceDescription = aDecoder.decodeObject(forKey:"sourceDescription") as? String
            url = aDecoder.decodeObject(forKey: "url") as? String
            category = aDecoder.decodeObject(forKey: "category") as? NewsAPISwift.Category
            language = aDecoder.decodeObject(forKey: "language") as? Language
            country = aDecoder.decodeObject(forKey: "country") as? Country

            if let sorts = aDecoder.decodeObject(forKey: "sortBysAvailable") as? [SortBy] {
                self.sortBysAvailable = sorts
            } else {
                return nil
            }
        }

        func encode(with aCoder: NSCoder) {
            aCoder.encode(id, forKey: "id")
            aCoder.encode(name, forKey: "name")
            aCoder.encode(sourceDescription, forKey: "sourceDescription")
            aCoder.encode(url, forKey: "url")
            aCoder.encode(category, forKey: "category")
            aCoder.encode(language, forKey: "language")
            aCoder.encode(country, forKey: "country")
            aCoder.encode(sortBysAvailable, forKey: "sortBysAvailable")    
        }    
    }
}

我创建了一个像这样的新结构:

let emptySource = NewsAPISource(id: "nil", name: "nil", sourceDescription: "nil", url: "nil", category: .business, language: .english, country: .unitedStates, sortBysAvailable: [.top])

并尝试对其进行编码:

emptySourceArray.encode()

这是导致异常的行。关于我做错的任何想法?

完整错误是:

  

***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [_ SwiftValue encodeWithCoder:]:无法识别的选择器发送到实例0x60800005f4d0'

0 个答案:

没有答案