Mostafa
Mostafa

Reputation: 1612

super init returns nil

I have this case where I just Call the init of Class I made this class is subclass of NSObject.

- (instancetype)initArray
{
    self = [super init];

    if (self) {
      //Do Some Logic
    }
return self;
}

This is my Call from the App Delegate

    CategoryLoader *categoryLoader = [[CategoryLoader alloc]initArray];

Whats driving me crazy is that:

Please note that both Xcode's are 6.3

Solutions i tried:

Here is a Screenshot of whats happening:

Screenshot

Any suggestions why could it be returning nil from NSObject and what can i do next ?

Thank you

Upvotes: 2

Views: 612

Answers (2)

Mostafa
Mostafa

Reputation: 1612

Ok. That was the problem:

The the scheme was on Release Mode. In this case the watch window displays nil in most of the objects. When I printed the value of self on NSLog it printed its value. The only difference between me and the other Xcode was the scheme.

So the solution is to edit the scheme of the project to be debug.

Thank you for your support

Upvotes: 1

Douglas Hill
Douglas Hill

Reputation: 1547

NSObject’s implementation of init should never return nil. This is documented in the NSObject Class Reference: although slightly confusing, the key part is

The init method defined in the NSObject class does no initialization; it simply returns self.

If you are observing it retuning nil, either something is not setup how you expect (perhaps the class isn’t a direct subclass of NSObject), or you are somehow interpreting the results incorrectly, or there is something wrong at another level. The fact that you observe different results on different machines suggests it might be at another level, like Xcode, the operating system, or the hardware. I can’t help much there; try rebooting.

Also, your method ought to be named init not initArray. This is not a requirement but a very strong convention. You can read about Object Initialization in Apple’s Concepts in Objective-C Programming

Upvotes: 1

Related Questions