0

我不断收到错误“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'+ [MainViewController minimumFormInContext:]:无法识别的选择器发送到类”来自这行代码:NSLog(@“访问特定的矿井实体”);

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];

使用调试器,我将错误范围缩小为:

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

我该如何解决?

4

1 回答 1

3

这很可能是一个保留/释放错误。在 XCode 中执行“构建和分析”,并改进您的代码以删除所有警告。

以下是我注意到的事情:

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

这两行非常糟糕。你的是什么mArraym代表,member还是? mutable如果它是一个成员变量,你不应该只为它分配一个新数组,如

// 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! 

此外,如果您像以前一样进一步分配 a mutableCopy

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

请注意,在 Objective-C 中,您在源代码中看到的变量只是一个指针,而不是对象本身。如果你给一个变量赋值,它不会让对象执行赋值操作,但它只是改变指针指向不同的东西。

你有这些线条的事实表明你在其他地方也有类似的东西;其中任何一个最终都可能导致您遇到的错误。所以你需要一一处理。祝你好运!

另一点:当您准备变量时,如果与您列出的任何选项都不匹配,则子句predicate链未定义。这是非常危险的,因为在 Objective-C 中,行ifpredicateregion

 NSPredicate* predicate;

不初始化为. _ 所以有可能predicatenil

[request setPredicate:predicate];

predicate会给of设置一个垃圾requrest。你应该把它改成

 NSPredicate* predicate=nil;
于 2010-09-05T23:04:39.720 回答