【发布时间】:2010-06-16 22:20:53
【问题描述】:
我试图在 nsdictionary ivar 上调用 objectForKey:,但我收到 EXC_BAD_ACCESS 错误。 nsdictionary 是使用 JSON 框架创建的,然后被保留。我第一次使用它(就在我创建它之后,相同的运行循环)它工作得非常好,但是当我稍后尝试访问它时没有任何效果。我正在执行此代码以尝试找出问题所在:
if (resultsDic == nil) {
NSLog(@"results dic is nil.");
}
if ( [resultsDic respondsToSelector:@selector(objectForKey:)] ) {
NSLog(@"resultsDic should respond to objectForKey:");
}
字典永远不会是 nil,但它总是在 respondsToSelector 上崩溃。有什么想法吗?
补充: 除了上述之外,这些是字典与之交互的其他地方:
- (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];
}
【问题讨论】:
-
您是否使用过调试器并验证
resultsDic仍然包含有效的引用/仍然是字典? -
它说有 0 个键/值对。但是他们怎么可能消失了,字典应该被保留?
标签: objective-c