views:

73

answers:

1

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?

+2  A: 

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:

mArray = [[NSMutableArray alloc] init];
mArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

These two lines are very bad. What's your mArray? Does m stands for member, or mutable? If it's a member variable, you shouldn't just assign a new array to that as in

// mArray points to an array at this time, say X
mArray = [[NSMutableArray alloc] init];
// at this point, mArray points to an array Y created by alloc init. X is lost! 

Moreover, if you further assign a mutableCopy as you did,

mArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
// at this point, mArray points to an array Z created by mutableCopy. Y is lost, too!

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 of if clauses leaves predicate undefined if region matches none of the choices you listed. This is very dangerous, because in Objective-C, the line

 NSPredicate* predicate;

does not initialize predicate to be nil. So it's possible that

[request setPredicate:predicate];

will set a garbage to the predicate of requrest. You should change it to

 NSPredicate* predicate=nil;
Yuji
I strongly suspect it's the latter — the invalid selector listed is an NSPredicate method. So probably the variable that he thinks points to an NSPredicate in fact points off to a random memory location.
Chuck
my mArray stands for mineArray. What I am trying to do is create an array of entities for use in the program temporarily; is there a better way to place data into an array from a fetch request? I have edited the predicates to equal nil before they are used.
kevin Mendoza
and it seems like setting them to nil is a no-no, so I just set them equal to some other predicate.
kevin Mendoza
Well, setting to `nil` is in general a good thing; I guess `NSFetchRequest` doesn't like `nil` predicate. I don't see any problem with your general approach. It's just about crushing the bugs.
Yuji
well I was able to fix it thanks to your array declaration help.
kevin Mendoza