iphone - Objective-C 正确复制 NSArray?

标签 iphone objective-c iphone-sdk-3.0

我的 iPhone 应用程序不断崩溃,在过去该死的一周里我已经将它缩小到这一行:

NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];

我从上面得到了正确的结果,但应用程序在访问它后崩溃了 (EXC_BAD_ACCESS)。 我如何复制 fetchResults 的内容以便我可以使用它?

我试过了

NSArray *retVal = [[NSArray alloc] initWithArray:fetchResults];
NSArray *retVal = [[NSArray alloc] initWithArray:[fetchResults copy]];
NSArray *retVal = [[NSArray alloc] initWithArray:[fetchResults retain]];

但唯一不会使应用程序崩溃的是

NSArray *retVal = [[NSArray alloc] initWithArray:nil];

有人能帮帮我吗?我想我需要上一堂 Obj-C 内存管理类(class)。

编辑: 下面是一个更完整的崩溃代码示例:

NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];
[request release];
NSMutableArray *retVal = [NSMutableArray arrayWithCapacity:0];
for(Job *job in fetchResults){
    //NSLog(@"dev: %@",job.lastmod_device);
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                            [job.jobkey copy], @"entitykey",
                            [NSNumber numberWithInt:[job.lastmod_device timeIntervalSince1970]], @"job_lastmod_device",
                            [NSNumber numberWithInt:[job.lastmod_server timeIntervalSince1970]], @"job_lastmod_server",
                            [NSNumber numberWithInt:[job.customer.lastmod_device timeIntervalSince1970]], @"customer_lastmod_device",
                            [NSNumber numberWithInt:[job.customer.lastmod_server timeIntervalSince1970]], @"customer_lastmod_server",
                            [NSNumber numberWithInt:[job.productionschedule_lastmod_device timeIntervalSince1970]], @"productionschedule_lastmod_device",
                            [NSNumber numberWithInt:[job.productionschedule_lastmod_server timeIntervalSince1970]], @"productionschedule_lastmod_server", nil];
    //NSLog(@"dict: %@", dict);
    [retVal addObject:dict];
}
return retVal;

以及调用该方法的代码:

NSArray *arr2 = [self retrieveJobs];

(就是这样;我什至从不使用变量)

编辑 2: 即使只是用一个空的 for 循环遍历获取的结果而不对 fetchResults 对象做任何其他事情也会导致应用程序崩溃。这怎么可能?

最佳答案

你在挣扎;这些代码行...

NSArray *retVal = [[NSArray alloc] initWithArray:fetchResults];
NSArray *retVal = [[NSArray alloc] initWithArray:[fetchResults copy]];
NSArray *retVal = [[NSArray alloc] initWithArray:[fetchResults retain]];

后两个是简单的泄漏。第一种是制作副本的一种方式,但 retVal = [fetchResults copy]; 是制作副本的更好方式。

当然,您根本不需要副本。那不是问题所在。您继续说唯一不会崩溃的是空结果集。

这表示两件事之一;您的结果集已损坏(不太可能),或者您正在错误地访问结果集(可能)。

“颠簸”是指您提出了有关崩溃的问题,但没有包括崩溃的回溯或崩溃的位置。添加这些,您可能很快就会得到答案。

(不要因为“颠簸”而生气——我们都这样做。即使在使用 Objective-C 20 多年之后,我仍然必须面对面的,退后一步,仔细考虑事情以避免颠簸。 )

关于iphone - Objective-C 正确复制 NSArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2338275/

相关文章:

iphone - Swift UIAlertController -> ActionSheet iPad iOS8 崩溃

iphone - Objective C 中 float 的随机范围?

html - 在 ios swift 中加载 html 字符串,UIWebView 除外

iPhone 核心数据 - 获取的托管对象未在设备上发布(在模拟器上很好)

ios - xib自动调整大小之谜?

iphone - 内存攀升,但代码中没有任何内存泄漏,发生在 iPhone 3G、SDK 3.0 中

ios - 如何在 UITableViewCell 中显示删除按钮而不像在 Moda Operandi iOS 应用程序中那样滑动/触摸它?

objective-c - UIWebView 不会在 Xcode6 中加​​载

ios - 如何在 epson tm-m30 打印机上设置自动同时打印 3-4 份

iphone - 使用已弃用的 setStatusBarHidden 方法时出现 Xcode 警告