Karsten
Karsten

Reputation: 1907

edge case with nullability in init function

I am refactoring some code for nullability and wondering about the edge case, when my parent init like that:

[super initWithData:data]

is returning nil on invalid data.

- (instancetype)initWithData:(NSData *)data{
     if ((self = [super initWithData:data])) {
        //some additional code
     } else {
        //invalid exit
        return nil;
     }
     return self;
}

Will this work or is there a better way for this problem?

Upvotes: 0

Views: 27

Answers (1)

Asperi
Asperi

Reputation: 257493

This will work, but you have to show this possibility in your interface as below, so clients of API be informed

- (nullable instancetype)initWithData:(NSData *)data;

Upvotes: 1

Related Questions