“Convenience initializer missing a 'self' call to another initializer”

岁酱吖の 提交于 2020-01-12 01:36:22

问题


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;
}

回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/24458608/convenience-initializer-missing-a-self-call-to-another-initializer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!