Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing Swift Generic Class with NSObject inheritance

I'm running into an error where I'm not certain if it's a limitation of the Swift language, or a bug. Here's the most basic premise:

class GenericClass<T> : NSObject {

    var inputValue: T

    init(value: T) {
        self.inputValue = value
        super.init()
    }
}

class SubClass : GenericClass<String> {

    override init(value: String) {
        super.init(value: value)
        }

  }

var test = GenericClass(value: "test") //Succeeds
var test2 = SubClass(value: "test2") //Fails with EXC_BAD_ACCESS

I'm getting no compiler warnings here, but the Subclass refuses to instantiate. I have a more complicated goal within subclassing specific contexts of a generic, but this basic issue above is what I boiled it down to.

Interestingly, if I remove the NSObject inheritance on the GenericClass (and the super.init() from the generic's init method), this setup works without issue, so I'm thinking it must have something to do with the fact that I'm inheriting from NSObject. My full implementation MUST inherit from an NSOperation (I'm basically making custom NSOperation classes with a generic superclass), so inheriting from NSObject(i.e. NSOperation) is not optional for me.

It's bothersome that there are no compiler errors and I'm getting something as nasty as a EXC_BAD_ACCESS. It makes me think that maybe this is supposed to work, but isn't currently. I know they only recently began supporting the subclassing of generic classes in Swift. I'm running the latest xCode beta 6.

Any insight appreciated!

like image 812
erendiox Avatar asked Sep 02 '15 00:09

erendiox


People also ask

Do Swift classes inherit from NSObject?

You don't have to inherit from NSObject in Swift, but you did in Objective-C and in fact there are some behaviors you can only have if you do inherit from it.

Can a generic class inherit?

You cannot inherit a generic type. // class Derived20 : T {}// NO!

What is NSObject in IOS Swift?

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.


1 Answers

I'm pretty sure it's a bug. Interestingly, everything works fine if you delete the initializers, even when you inherit from NSObject:

class GenericClass<T> : NSObject {

}

class SubClass : GenericClass<String> {

}

var test : GenericClass<Int> = GenericClass() // Succeeds
var test2 = SubClass() // Succeeds
var test3 : GenericClass<String> = SubClass() // Succeeds
var test4 : GenericClass<Int> = SubClass() // Fails ("cannot convert SubClass to GenericClass<Int>")

A messy workaround might be to use default protocol implementations, and then extend NSOperation to initialize a new operation from your protocol, but it's probably better to just file a bug report and wait for them to fix this :)

like image 183
Aaron Brager Avatar answered Sep 24 '22 11:09

Aaron Brager