NSKeyedArchiver does not work in Swift Generic Classes

Originator:thomas.zimmer
Number:rdar://21813211 Date Originated:14-Jul-2015
Status:open Resolved:
Product:ios Product Version:
Classification:crash Reproducible:yes
 
As soon as any new Swift Class has a generic parameter, the class cannot be archived with the NSKeyedArchiver. The code generates an exception because it cannot find the method encodeWithCoder(..) , even when it is correctly coded in the generic class.

Steps to Reproduce:
Sample Code:

class Foo<T> : NSObject
{
    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")


Generates an Exception.


Actual Results:
StoreSample[28906:2203848] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtC11StoreSample5Store00007F99796119B0 encodeWithCoder:]: unrecognized selector sent to instance 0x7f9979611ec0'

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!