The designated initializer should only call the designated initializer on 'super'

This code is a modified version of the Tempered Dictionary implemented here. https://github.com/nicklockwood/OrderedDictionary/tree/master/OrderedDictionary

Interface -> OrderedDictionary.h

 @interface OrderedDictionary : NSMutableDictionary
{
}

Implementation -> OrderedDictionary.m

// This is the new method added.
- (instancetype)init
{
    return [self initWithCapacity:0];
}

- (instancetype)initWithCapacity:(NSUInteger)capacity
{
    self = [super init];
    if (self != nil)
    {
       // Allocate here.
    }
    return self;
}

The code works fine, but I get the following warnings in "- (instancetype) init".

  • A designated initializer should only call a designated initializer on 'super'
  • The designated initializer skips the "super" call the designated initializer of the superclass

What am I doing wrong and how to fix it?

The following code changes have been made to resolve the issue.

 // This is the new method added.

- (instancetype)init
{
    self = [super init];
    if (self != nil)
    {
       // Allocate here.
    }
    return self;
}

- (instancetype)initWithCapacity:(NSUInteger)capacity
{
    self = [super initWithCapacity:capacity];
    if (self != nil)
    {
       // Allocate here.
    }
    return self;
}
+4
4

NSMutableDictionary, :

  • initWithCapacity:
  • init

initWithCapacity super.init. , .

:

// This is the new method added.
- (instancetype)init
{
    return [self initWithCapacity:0];
}

- (instancetype)initWithCapacity:(NSUInteger)capacity
{
    self = [super initWithCapacity:capacity];
    if (self != nil)
    {
       // Allocate here.
    }
    return self;
}
+1

  - (instancetype) initWithCapacity: (NSUInteger) NS_DESIGNATED_INITIALIZER;

@interface. - initWithCapacity: , init . , . , ...

NSMutableDictionary , NSMutableDictionary . .

+3

.

,

self = [super init]; 

self = [super initWithCapacity:capacity];
0

, , Xcode "Modern Objective-C Syntax."

I saw cases where Objective-C header files were modified by the converter to have two initializers assigned:

- (instancetype) init NS_DESIGNATED_INITIALIZER;                                    // DO NOT USE
- (instancetype) initWithFileset: (NSArray *) fileset NS_DESIGNATED_INITIALIZER;    // use this to instantiate

If you change this code to a single initializer, you may find a warning:

- (instancetype) init;                                                              // DO NOT USE
- (instancetype) initWithFileset: (NSArray *) fileset NS_DESIGNATED_INITIALIZER;    // use this to instantiate

In short, check the header and source file when you see this warning message.

0
source

All Articles