iosobjective-cinitializationinstancetype

Are these two instancetype initialization methods equal?


I was wondering are these two equal:

A single parentheses inside the if statement:

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

Double parentheses inside the if statement:

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

Solution

  • Yes,...... as it only depends on the the expression being compared no matter how many parentheses exists (that should be even)

    Parentheses help compiler understand the expressions with prioritization , if statement in objective c needs at least one () while adding more will work also but it's useless