获得无法识别的选择器-replacementObjectForKeyedArchiver:在Swift中实现NSCoding时崩溃 [英] Got Unrecognized selector -replacementObjectForKeyedArchiver: crash when implementing NSCoding in Swift

查看:1363
本文介绍了获得无法识别的选择器-replacementObjectForKeyedArchiver:在Swift中实现NSCoding时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个符合NSCoding的Swift类。 (Xcode 6 GM,Swift 1.0)

I created a Swift class that conforms to NSCoding. (Xcode 6 GM, Swift 1.0)

import Foundation

private var nextNonce = 1000

class Command: NSCoding {

    let nonce: Int
    let string: String!

    init(string: String) {
        self.nonce = nextNonce++
        self.string = string
    }

    required init(coder aDecoder: NSCoder) {
        nonce = aDecoder.decodeIntegerForKey("nonce")
        string = aDecoder.decodeObjectForKey("string") as String
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeInteger(nonce, forKey: "nonce")
        aCoder.encodeObject(string, forKey: "string")
    }
}

但是当我打电话给...

But when I call...

let data = NSKeyedArchiver.archivedDataWithRootObject(cmd);

崩溃给了我这个错误。

2014-09-12 16:30:00.463 MyApp[30078:60b] *** NSForwarding: warning: object 0x7a04ac70 of class '_TtC8MyApp7Command' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[MyApp.Command replacementObjectForKeyedArchiver:]

我该怎么办?

推荐答案

虽然Swift类没有继承,但为了使用 NSCoding ,你必须继承自 NSObject

Although Swift class works without inheritance, but in order to use NSCoding you must inherit from NSObject.

class Command: NSObject, NSCoding {
    ...
}

太糟糕了,编译器错误的信息量不大:(

Too bad the compiler error is not very informative :(

这篇关于获得无法识别的选择器-replacementObjectForKeyedArchiver:在Swift中实现NSCoding时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆