9

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!

erendiox
  • 91
  • 4

2 Answers2

2

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

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks for the reply Aaron. I agree, it does smell like a bug. I've submitted a bug report. We'll see what happens :) Will give your protocol idea a shot in the meantime. – erendiox Sep 02 '15 at 17:38
  • Yes, I believe this was indeed a bug. It is now fixed in Swift 3, and there was no mention of it in the Swift 3 Language Changes: https://swift.org/blog/swift-3-0-released/ – ganzogo Nov 17 '16 at 09:26
1
import Foundation

class GenericClass<T> : NSObject {

    var inputValue: T

    init(value: T) {
        print(value)
        inputValue = value
        super.init()

    }
}

class SubClass<T> : GenericClass<T> {

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

}

let test = GenericClass(value: "test") //Succeeds
test.inputValue
let test2 = SubClass(value: "test2")   //Succeeds
test2.inputValue
let test3 = GenericClass(value: 3.14)  //Succeeds
test3.inputValue
let test4 = SubClass(value: 3.14)      //Succeeds
user3441734
  • 16,722
  • 2
  • 40
  • 59