gDev gDev - 7 months ago 41
Swift Question

Swift 2.2: Type '()' does not conform to protocol 'BooleanType'

I have converted an Objective-C method into Swift. And then, into the Swift code i am getting this error. I have no tied what i have done wrong.

Objective-C Code

- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
[self loadChecklistItems];
}
return self;
}


Swift 2.2 code

convenience required init(coder aDecoder: NSCoder) {
if (self.init(coder: aDecoder)) { // ERROR Here
self.loadChecklistItems()
}
}


Here is the screenshot of Swift code

Please tell me what i have done wrong and what will be the right code.

Answer

Just call the initializer directly:

convenience required init(coder aDecoder: NSCoder) {
    self.init(coder: aDecoder)
    self.loadChecklistItems()
}

However, note that your code is making an infinite cycle of calls. Are you sure you don't want to call super.init(...)? Also, I think that this is not a convenience initializer:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.loadChecklistItems()
}