Swift: odd XCode behavior with required initializer in NSArray subclass
Problem: I have a class that is a subclass of NSArray, not directly, but it is. The structure is more or less like this MyClass -> Class1 -> Class2 -> NSArray
.
Everything except MyClass
is in Objective-C, MyClass is in Swift. It worked well until I upgraded to Yosemite and Xcode 6.1.
Now at compile time it throws an error
'required' initializer 'init(arrayLiteral:)' must be provided by subclass of 'NSArray'
This is rather strange because there are other classes, siblings, MyClass
without the compiler complaining about them. When I add an initializer,
required convenience init(arrayLiteral elements: AnyObject...) {
fatalError("not implemented")
}
Xcode throws another error saying Declarations from extensions cannot be overridden yet
.
Does anyone know what I can do? The code has zero changes at all.
source to share
You may simply not see compilation errors in other files, as compilation tries to stop at the first problematic file.
If you do not specify any designated initializers in your subclass, or if you override all designated initializers but not convenience initializers, the problematic initializer is automatically inherited. In this case, it compiles fine:
class MyArray1: NSArray {
override init() { fatalError("todo") }
override init(objects: UnsafePointer<AnyObject?>, count cnt: Int) { fatalError("todo") }
required init(coder aDecoder: NSCoder) { fatalError("todo") }
}
class MyArray2: MyArray1 {
}
It compiles very easily on Xcode 6.1.
See Automatic Initializer Inheritance in the Quick Programming Guide.
source to share