繁体   English   中英

如何在Swift中的泛型类上实现NSCoding?

[英]How to implement NSCoding on a generic class in Swift?

我在Swift中使用泛型类和NSCoding(XCode beta 5)时遇到了问题。 具体来说,这个示例代码很好地工作:

class Foo : NSObject, NSCoding
{
    let bar: String
    init(bar: String){
        self.bar = bar;
    }

    func encodeWithCoder(aCoder: NSCoder!){
        aCoder.encodeObject(bar, forKey: "bar")
    }
    required init(coder aDecoder: NSCoder!){
        self.bar = aDecoder.decodeObjectForKey("bar") as String
        super.init()
    }


}

let foo = Foo(bar: "hello, world")

NSKeyedArchiver.archiveRootObject(foo, toFile: "test.dat")

但是,当我尝试相同的代码,并添加一个通用参数; 当我尝试编码对象时; 我收到一个'无法识别的选择器发送到实例'错误:

class Foo<T> : NSObject, NSCoding
{
    let bar: String
    init(bar: String){
        self.bar = bar;
    }

    func encodeWithCoder(aCoder: NSCoder!){
        aCoder.encodeObject(bar, forKey: "bar")
    }
    required init(coder aDecoder: NSCoder!){
        self.bar = aDecoder.decodeObjectForKey("bar") as String
        super.init()
    }


}

let foo = Foo<String>(bar: "hello, world")

NSKeyedArchiver.archiveRootObject(foo, toFile: "test.dat")

这是错误详细信息和堆栈跟踪:

[_TtC6SafeId14ArrayContainer00007F8893418E58 encodeWithCoder:]: unrecognized selector sent to instance 0x7f8890ee4080
2014-08-16 10:43:54.632 SafeId[1778:32501] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtC6SafeId14ArrayContainer00007F8893418E58 encodeWithCoder:]: unrecognized selector sent to instance 0x7f8890ee4080'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000103a6e3e5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010371e967 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000103a754fd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001039cd5ff ___forwarding___ + 495
    4   CoreFoundation                      0x00000001039cd388 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00000001032eaa75 _encodeObject + 1120
    6   Foundation                          0x0000000103326bf5 +[NSKeyedArchiver archiveRootObject:toFile:] + 241

当我读到这个时,当类是通用的时,Swift无法调用encodeWithCoder的实现。 它是否正确 ? 为了在泛型类上使用NSCoding,我该如何解决这个问题?

在使类成为通用的那一刻,它变得与Objective-C不兼容。 NSCoder的所有内容都发生在Objective-C中,后者无法访问您的课程。

请注意,使类成为通用的还有其他后果。 例如,如果要添加description属性,则还会出现编译时错误:

// Error: '@objc' getter for non-'@objc' property
override var description: String {
    return "Foo: \(bar)"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM