How to implement NSCoding on a generic class in Sw

2020-06-08 14:41发布

问题:

I am having trouble with generic classes and NSCoding in Swift (XCode beta 5). Specifically, this example code works nicely:

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")

However, when I try the same code, and add a generic parameter; when I try to encode the object; I get a 'unrecognized selector sent to instance' error:

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")

Here is the error details and stack trace:

[_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

As I read this, Swift is unable to call the implementation of encodeWithCoder when the class is generic. Is this correct ? How can I work around this in order to use NSCoding on generic classes ?

回答1:

The moment you make the class generic, it becomes non-compatible with Objective-C. The NSCoder stuff all happens in Objective-C which can no longer access your class.

Note there are other consequences of making the class generic as well. For example, if you were to add a description property, you'll also get a compile-time error:

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