我不断收到错误“由于未捕获的异常终止应用程序'NSInvalidArgumentException',原因:'+ [MainViewController minimalFormInContext:]:无法识别的选择器发送到类” 从这行代码: 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];
我该如何解决这个问题?
答案 0 :(得分:3)
这可能是一个保留/释放错误。在XCode中执行“构建和分析”,并改进代码以删除所有警告。
以下是我注意到的事情:
mArray = [[NSMutableArray alloc] init];
mArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
这两行非常糟糕。你的mArray
是什么? m
代表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!
此外,如果您进一步指定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
时,如果if
与您列出的选项都不匹配,则predicate
子句链将保留region
。这是非常危险的,因为在Objective-C中,行
NSPredicate* predicate;
不将predicate
初始化为nil
。所以有可能
[request setPredicate:predicate];
会将垃圾设置为predicate
的{{1}}。你应该把它改成
requrest