If i call a method inside its Class it works. Calling it from out the class causes a crash
When I do this my app crashes without any error.
footballScores = [[FootBallScores alloc] init];
[footballScores initStuff];
I call this from my viewDidLoad of my view controller.
But if i comment out
footballScores = [[FootBallScores alloc] init];
//[footballScores initStuff];
And move the method call initStuff into the init method inside the class FootballScores it all works开发者_开发知识库 perfectly.
So it seems for some reason called initStuff from outside the class is causing my app to crash.
Anybody able to educate me as to why this happens?
Many Thanks, Code
EDITS
-(void) init
{
//[self initStuff];
}
- (void)initStuff // Was - (void)awakeFromNib
{
NSLog(@" init called");
appDelegate = (FootballRadioAppDelegate *)[[UIApplication sharedApplication]
delegate];
responseData = [[NSMutableData data] retain];
baseURL = [[NSURL
URLWithString:@"http://footballmatchcentre.timesonlinesport.co.uk/ifb2009/league.asp?
league=EPL"] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
}
The body of init gets commented out to test the call from outside the class and i uncomment it to test if it works by getting called from within the class.
Your -init method should return self, of type id, instead of void. It should attribute [super init] to self. A standard -init implementation would be:
- (id)init {
self = [super init];
if (self != nil) {
// initialisation code
}
return self;
}
Then you can have a -(void)initStuff method and send [self initStuff] inside that if block that tests whether self != nil.
精彩评论