Angus
Angus

Reputation: 12621

cannot init a class object-Objective C

I need to call a method that has been defined in the same class.I am calling the method initiateSession from the function A.

-(BOOL)intitiateSession
{
 //stmts
 return YES;
}
-(BOOL)A
{
 if([self initiateSession])
 {
  //stmts
 }
 return YES;
}

getting a runtime error as,

Terminating app due to uncaught exception

cannot init a class object

EDITED:

@implementation Data
-(BOOL)initiateSession
{
NSLog(@"I am into the initiateSession!!");
return YES;
}
-(BOOL)getNHPData{
        NSLog(@"In getNHPData!!");
        if([self initiateSession])
                   {
                       NSLog(@"I am into the session");
                   }
     return YES;
}
@end

Here in the if loop i will call the function.Execute the function and if the function

executes to true i'm getting inside the scope.I could not able to call one function from

other function.

Upvotes: 0

Views: 6608

Answers (3)

DCN
DCN

Reputation: 97

Always do a super init call, when you override init in your class.

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299265

getNHPData() is not an initializer and should not be calling [super init]. That is something you would call from within your own initializer. Your code as written will not even compile (your @implementation line is incorrect). You are certainly receiving many warnings in the above code that you should be listening to (you're returning self from a BOOL method in one case, and have no return in the other case). Your actual error suggests you're doing something like [Data getNHPData] which is not legal since getNHPData is an instance method. You should be getting warnings about that as well.

First, clean up all your warnings. That will probably get you moving in the right direction. Cocoadevcentral has a quick intro to all the issues you're running into.

Upvotes: 3

gcamp
gcamp

Reputation: 14662

You need to call [super init] in your overwrite of -(id) init.

See #3 and #6 here.

Upvotes: 1

Related Questions