Why does ((self = [super init]]) work, but (! (Self = [super init])) doesn't?

For aesthetic reasons, I decided to change this:

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

In it:

if (!(self = [super init])) return nil;
// init self
return self;

In theory, they do the same. The first is the classic way, it just works. After debugging the second, I found that it almost worked. "If" does this correctly, the initialization code also, but after returning "I" the debugger returns to "if" and returns nil!

All the classes that I did with the second one, I come back to use the "right" way, because they are where initing is with nil, but I really want to know why it behaves like this! I am afraid that this may be the result of something else!

+5
source share
2

, , - . , init?

+3

init:

- (id)init
{
    if (!(self = [super init])) return nil;
    [self setText:@"foo"];
    return self;
}

, , text. , , - .

+1

All Articles