kevin Mendoza
kevin Mendoza

Reputation: 1221

Having trouble copying data to a mutable array

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?

Upvotes: 0

Views: 1057

Answers (1)

Yuji
Yuji

Reputation: 34185

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;

Upvotes: 3

Related Questions