Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Convenience initializer missing a 'self' call to another initializer"

Tags:

I'm trying convert my code to iOS 8 project, and i need some explanation on how to fix this warning: "Convenience initializer missing a 'self' call to another initializer"

on this code:

-(instancetype) initWithCoder:(NSCoder *)aDecoder // warning: Convenience initializer missing a 'self ' call to another initializer
{
    if (self = [super initWithCoder:aDecoder]) // warning: convenience initializer should not invoke an initializer on 'super'
    {
    // some init stuff here
    }
    return self;
}
like image 714
ignotusverum Avatar asked Jun 27 '14 18:06

ignotusverum


People also ask

What must a convenience initializer call?

The convenience initializer must call one of the two designated initializers, because it can only call another initializer from the same class. This satisfies rules 2 and 3 from above. Both designated initializers must call the single designated initializer from the superclass, to satisfy rule 1 from above.

Why do we need convenience initializer of the same task can be obtained with designated initializer?

A convenience initializer is a secondary initializer that must call a designated initializer of the same class. It is useful when you want to provide default values or other custom setup. A class does not require convenience initializers.

What is use of convenience Initializers?

Designated initializers are the default way of creating new instances of a type. There are others, known as convenience initializers, that are there to help you accomplish common tasks more easily, but those are in addition to your designated initializers rather than a replacement.

What is convenient init?

convenience init : Convenience initializers are secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer's parameters set to default values.


2 Answers

The new Clang shipping with Xcode 6 enables compiler-enforced designated initializers through the NS_DESIGNATED_INITIALIZER macro. When it marks any one of the init-family methods in a class's declaration, all other initializers are considered "secondary" (to use Apple's terminology) initializers. That is, they should call through to one another designated or secondary initializer until they reach a designated initializer.

UIView marks nothing as the designated initializer, so somewhere you've declared another init method of the class as the designated initializer. Because of that, NSCoder's initializer becomes marked as secondary and generates a warning. I've filed a radar (rdar://17559176) about it, but until then it can be turned off on a per-file basis by specifying -Wno-objc-designated-initializers, or by providing the appropriate diagnostic push-pop with -Wobjc-designated-initializers.

like image 149
CodaFi Avatar answered Sep 20 '22 14:09

CodaFi


Just an addendum to what @CodaFi is saying:

  • One slice of Apple docs I found on designated initializers is here.
  • I've answered a question on how to suppress these warnings here, with a couple more options. I particularly like the precompiled header method (#2), because it reminds you to fix it.
like image 39
Clay Bridges Avatar answered Sep 18 '22 14:09

Clay Bridges