Reputation: 446
I have a class FbPostViewCell
which inherits a UITableViewCell
class. From FbPostViewCell
I makes other classes: FbLinkPostViewCell
. How can I init FbPostViewCell
substance of FbLinkPostViewCell
object in specific manner? I mean I need to init members of FbPostViewCell
instance of FbLinkPostViewCell
object.
I wrote a convenience init() { super.init() /* stuff */ }
but program behaviour showed to me that my members weren't initialized. Same situation with required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) /* same stuff */ }
. What I did wrong?
Upvotes: 0
Views: 693
Reputation: 2469
If you have added stored properties to your subclass you will have to initiate them in some way. This means you should either: - Make all added properties optional, this way they can stay without value - Give all properties an initial value - Add a custom initializer
this custom initializer should be just a regular init which calls super.init() and initializes all the properties. If you have added this init you can always create an object of your subclass type by initiating with your own convenience initializer. But this convenience initializer should call self.init().
Hope this helps, relevant page in Apple Swift guide: https://developer.apple.com/library/watchos/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203
EDIT:
class FbPostViewCell : UITableViewCell {
init(maybe variables) {
super.init() //inits UITableViewCell properties
/* init fbpostviewcell properties with 'maybe variables' */
}
}
class FbLinkPostViewCell : FbPostViewCell {
init(more variables) {
super.init(maybe variables) //inits UItvc properties and fbpostviewcell's
/* init fblinkpostviewcells properties with more variables */
}
convenience init(postMode?) {
if let mode = postMode {
switch mode {
case mode1: self.init(mode1 specific vars)
case mode2: self.init(mode2 speciific vars)
default: self.init(default vars)
}
else {
self.init(no mode vars)
}
}
}
Upvotes: 1