Hi all.
Note: I am using the latest version of Xcode and Swift to date (official releases.)
Here is a scenario. ClassA (a framework class) defines a convenience initializer. Subclass ClassB (my custom class) defines a new optional property and a designated initializer for it. (As per the Swift Programming Language 2.1, defining an initializer for the subclass means that none of the superclass's initializers will be inherited.)
Now, what I want to be able to do is create an instance of ClassB using the convenience initializer from ClassA. Is this possible? If so, how?
I cannot override the convenience initializer because, being a framework class, I don't know what it does. And I can't call a convenience initializer from a superclass as per the rules of the language.
class BaseClass {
var property1 = 0
init(property1: Int) {
self.property1 = property1
}
convenience init(value1: Int, value2: Int) {
// Super secret formula hidden in a frame work that I don't have access to.
self.init(property1: value1 + value2)
}
}
class SubClass: BaseClass {
var property2: Int?
init(property2: Int) {
super.init(property1: 10)
self.property2 = property2
}
}
let c1 = SubClass(property2: 4)
print(c1.property2!)
// This part won't compile. What can be done to get this to work?
let c2 = SubClass(value1: 5, value2: 5)
print(c2.property1)
For a real-world scenario, try subclassing SpriteKit's SKScene and provide at least 1 custom initializer and then try to create an instance of your SKScene subclass using the fileNamed: initializer. When I try, ultimately I get an "Incorrect argument label in call (have 'fileNamed:', expected 'coder:')" as I cannot figure out how to implement fileNamed: in my subclass. Funny thing is, fileNamed: is a convenience initializer defined up the chain in SKNode and SKScene does define its own initializer init(size: CGSize). Unless I'm mistaken, SKScene is a class that successfully does what I am looking to do (because you can create an instance of SKScene using fileNamed:) How to extend that ability to my custom SKScene subclass is a mystery to me.
Any help would be appreciated. Thanks.