将数据复制到可变数组时遇到问题
我不断收到错误“由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:‘+[MainViewControllerimalFormInContext:]:发送到类的无法识别的选择器” 从这行代码: 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];
如何修复此问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能是一个保留/释放错误。在 XCode 中进行“构建和分析”,并改进代码以删除所有警告。
以下是我注意到的事情:
这两行非常糟糕。你的
mArray
是什么?m
代表member
还是mutable
?如果它是一个成员变量,您不应该像在中那样只为其分配一个新数组此外,如果您像您那样进一步分配一个
mutableCopy
,请注意,在 Objective-C 中,您在源代码只是一个指针,而不是对象本身。如果将某些内容分配给变量,它不会使对象执行分配操作,而只是更改指针以指向不同的内容。
事实上,你有这些线条表明你在其他地方也有类似的东西;其中任何一个最终都可能导致您遇到的错误。所以你需要一一处理。祝你好运!
另一点:当您准备变量
predicate
时,如果region
不匹配,则if
子句链会使predicate
未定义您列出的选项。这是非常危险的,因为在 Objective-C 中,该行不会将
谓词
初始化为nil
。因此,可能会为
requrest
的predicate
设置垃圾。你应该将其更改为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
? Doesm
stands 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
mutableCopy
as 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 ofif
clauses leavespredicate
undefined ifregion
matches none of the choices you listed. This is very dangerous, because in Objective-C, the linedoes not initialize
predicate
to benil
. So it's possible thatwill set a garbage to the
predicate
ofrequrest
. You should change it to