I keep getting the error “Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘+[MainViewController minimalFormInContext:]: unrecognized selector sent to class”
from this line of code:
NSLog(@”Accessing specific mine entities”);
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Mine" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSError *error = nil;
[request setEntity:entity];
NSPredicate *predicate;
NSPredicate *metalFilter;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *region = [defaults stringForKey:@"mineArray"];
if([region isEqualToString:@"Butte & Plumas"])
{
predicate = [NSPredicate predicateWithFormat:@"(county Contains %@) OR (county Contains %@)",@"Butte",@"Plumas"];
}
else if([region isEqualToString:@"Sutter, Yuba, & Sierra"])
{
predicate = [NSPredicate predicateWithFormat:@"(county Contains %@) OR (county Contains %@) OR (county Contains %@)",@"Sutter",@"Yuba",@"Sierra"];
}
else if([region isEqualToString:@"Nevada & Placer"])
{
predicate = [NSPredicate predicateWithFormat:@"(county Contains %@) OR (county Contains %@)",@"Nevada",@"Placer"];
}
else if([region isEqualToString:@"Sacramento & El Dorado"])
{
predicate = [NSPredicate predicateWithFormat:@"(county Contains %@) OR (county Contains %@)",@"Sacramento",@"El Dorado"];
}
else if([region isEqualToString:@"San Joaquin, Amador, & Calaveras"])
{
predicate = [NSPredicate predicateWithFormat:@"(county Contains %@) OR (county Contains %@) OR (county Contains%@)",@"San Joaquin",@"Amador", @"Calaveras"];
}
else if([region isEqualToString:@"Tuolumne & Stanislaus"])
{
predicate = [NSPredicate predicateWithFormat:@"(county Contains %@) OR (county Contains %@)",@"Tuolumne",@"Stanislaus"];
}
else if([region isEqualToString:@"Merced, Mariposa, & Madera"])
{
predicate = [NSPredicate predicateWithFormat:@"(county Contains %@) OR (county Contains %@) OR (county Contains %@)",@"Merced",@"Mariposa",@"Madera"];
}
[request setPredicate:predicate];
mArray = [[NSMutableArray alloc] init];
mArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
using debugger, I have narrowed down the error as occurring in:
mArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
How do I fix this?
It’s likely that it’s a retain/release bug. Do “Build and Analyze” in XCode, and improve your code to remove all of the warnings.
Here are things I noticed:
These two lines are very bad. What’s your
mArray? Doesmstands formember, ormutable? If it’s a member variable, you shouldn’t just assign a new array to that as inMoreover, if you further assign a
mutableCopyas you did,Note that in Objective-C, the variables you see on the source code is just a pointer, not the object itself. If you assign something to a variable, it doesn’t make the object perform the assign operation, but it just changes the pointer to point to something different.
The fact that you have these lines suggests you have similar things in various other places; any of it can eventually lead to the bug you’re encountering. So you need to deal with them one by one. Good luck!
Another point: when you prepare the variable
predicate, the chain ofifclauses leavespredicateundefined ifregionmatches none of the choices you listed. This is very dangerous, because in Objective-C, the linedoes not initialize
predicateto benil. So it’s possible thatwill set a garbage to the
predicateofrequrest. You should change it to