将数据复制到可变数组时遇到问题

发布于 2024-09-17 23:18:55 字数 2363 浏览 7 评论 0原文

我不断收到错误“由于未捕获的异常‘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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2024-09-24 23:18:55

这很可能是一个保留/释放错误。在 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 时,如果 region 不匹配,则 if 子句链会使 predicate 未定义您列出的选项。这是非常危险的,因为在 Objective-C 中,该行

 NSPredicate* predicate;

不会谓词初始化为nil。因此,可能会

[request setPredicate:predicate];

requrestpredicate 设置垃圾。你应该将其更改为

 NSPredicate* predicate=nil;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文