Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSDictionary导致EXC\u访问错误_Objective C - Fatal编程技术网

Objective c NSDictionary导致EXC\u访问错误

Objective c NSDictionary导致EXC\u访问错误,objective-c,Objective C,我试图在nsdictionary ivar上调用objectForKey:但我得到了一个EXC\u BAD\u访问错误。 nsdictionary使用JSON框架创建,然后保留。我第一次使用它(就在我创建它之后,相同的运行循环)时,它工作得非常好,但是当我稍后尝试访问它时,没有任何效果。我编写此代码是为了找出错误: if (resultsDic == nil) { NSLog(@"results dic is nil."); } if ( [resultsDic respondsToSe

我试图在nsdictionary ivar上调用objectForKey:但我得到了一个EXC\u BAD\u访问错误。 nsdictionary使用JSON框架创建,然后保留。我第一次使用它(就在我创建它之后,相同的运行循环)时,它工作得非常好,但是当我稍后尝试访问它时,没有任何效果。我编写此代码是为了找出错误:

if (resultsDic == nil) {
    NSLog(@"results dic is nil.");
}
if ( [resultsDic respondsToSelector:@selector(objectForKey:)] ) {
    NSLog(@"resultsDic should respond to objectForKey:");
}
字典从不为零,但它总是在响应选择器时崩溃。有什么想法吗

补充: 除上述内容外,字典还与其他地方进行了互动:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [connection release];

    //get the data in a usable form
    NSString *jsonString = [[NSString alloc] initWithData:downloadedData encoding:NSUTF8StringEncoding];
    resultsDic = [jsonString JSONValue];

    [self processResults];

    NSLog(@"Success. Received %d bytes of data",[downloadedData length]);
    [downloadedData release];
    [jsonString release];
}



- (void)processResults
{
        NSArray *resultsArr = [resultsDic objectForKey:@"results"];
                CLLocationCoordinate2D coordinate = [self coordinateFromResult:[resultsArr objectAtIndex:0]];
        NSLog(@"lat: %f lng: %f", coordinate.latitude, coordinate.longitude);
}

- (void)dealloc {
  [resultsDic release];
  [super dealloc];
}

somethings retain count减少到0后,对象将被释放。这与将其设置为零不同。不会是零。虽然您可以向nil发送消息,但向已释放的对象发送消息将导致EXC_BAD_访问错误。如果您在创建和使用它的地方发布一些代码,也许我们可以帮助您调试它。开始时试着保留两次。这不是一个优雅的解决方案,但可能是一个快速解决方案。

听起来像是一个经典的僵尸。再次运行它,将环境变量
NSZombieEnabled
设置为
YES
(或使用Instruments.app中的Zombies工具)。这将为您提供更多关于正在发生的事情的信息。

您是否使用了调试器并验证了
resultsDic
仍然包含有效引用/仍然是字典?它说有0个键/值对。但是他们怎么会离开呢,这本词典应该保留下来吗?啊,谢谢你的解释。创建后立即添加一个retain使其工作,现在我需要检查是否存在泄漏。僵尸工具对我来说是灰色的,但我会让它运行起来。根据泄漏工具,字典没有泄漏。这是否意味着我无论如何都应该保留它,并且我添加的保留是必要的?很可能。如果每个人都遵守了正确的规则,那么您得到的词典将被自动删除(这就是为什么您能够立即使用它,但它后来被释放)。所以如果你想保留它,就保留它。