Code crash with alloc, init
I have an iOS application that selects ARC option when the application is
firstly created.
I have a question about some coding way that causes some crash. I do not
know why the code crash happens if I declare and set memory allocation for
a variable in one code line, and then assigning real value for that
variable in another line.
Thank you for your help.
// if I use one line of code as follows, then I do NOT have code crash
TrainingCourse* course = [results objectAtIndex:0];
// BUT, if I separate the code line above into the 2 line of codes as
follows, I get code crash
TrainingCourse* course = [[TrainingCourse alloc] init];
course = [results objectAtIndex:0]; // crash appears here
The full method:
-(TrainingCourse*) getTrainingCourseWithId:(NSNumber*)trainingCourseId
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription
entityForName:@"TrainingCourse"
inManagedObjectContext:self.managedObjectContext]];
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"trainingCourseId == %@", trainingCourseId];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *results = [self.managedObjectContext
executeFetchRequest:request error:&error];
// if I use one line of code as follows, then I do NOT see any code crash
TrainingCourse* course = [results objectAtIndex:0];
// BUT, if I separate the code line above into the 2 line of codes as
follows, I get code crash
// TrainingCourse* course = [[TrainingCourse alloc] init];
// course = [results objectAtIndex:0]; // crash appears here
return course;
}
No comments:
Post a Comment