iphone - 应用程序因委托(delegate)而崩溃

标签 iphone objective-c ios

我的一个 .m 类中有以下代码,

- (void)viewDidLoad {
    mapViewController = [[NeighborMapViewController alloc] init];
    self.navigationItem.title = @"Neighbors";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    UIBarButtonItem * test = [[UIBarButtonItem alloc] initWithTitle:@"Map" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleAction:)];
    self.navigationItem.rightBarButtonItem = test;  
    [super viewDidLoad];
    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.someurl.com"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    //label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}

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

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSArray *address = [responseString JSONValue];

    NSMutableString *text = [NSMutableString stringWithString:@"User address:\n"];

    for (int i = 0; i < [address count]; i++)
        [text appendFormat:@"%@\n", [address objectAtIndex:i]];

    NSLog(@"%@", text);
    //label.text =  text;
}

应用程序在模拟器中运行时就关闭了,我猜这是因为代理...但我可以看到一切运行良好。我的控制台出现以下错误:

2011-01-26 20:33:45.000 NeighborMe[2862:207] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x89119f0
2011-01-26 20:33:45.073 NeighborMe[2862:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x89119f0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x027d9b99 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0292940e objc_exception_throw + 47
    2   CoreFoundation                      0x027db6ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x0274b2b6 ___forwarding___ + 966
    4   CoreFoundation                      0x0274ae72 _CF_forwarding_prep_0 + 50
    5   NeighborMe                          0x0000c0c4 -[NeighborListViewController connectionDidFinishLoading:] + 275
    6   Foundation                          0x0007cb96 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108
    7   Foundation                          0x0007caef _NSURLConnectionDidFinishLoading + 133
    8   CFNetwork                           0x02d8d72f _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 285
    9   CFNetwork                           0x02e58fcf _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 389
    10  CFNetwork                           0x02e5944b _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1537
    11  CFNetwork                           0x02d82968 _ZN19URLConnectionClient13processEventsEv + 100
    12  CFNetwork                           0x02d827e5 _ZN17MultiplexerSource7performEv + 251
    13  CoreFoundation                      0x027bafaf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    14  CoreFoundation                      0x0271939b __CFRunLoopDoSources0 + 571
    15  CoreFoundation                      0x02718896 __CFRunLoopRun + 470
    16  CoreFoundation                      0x02718350 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x02718271 CFRunLoopRunInMode + 97
    18  GraphicsServices                    0x030b800c GSEventRunModal + 217
    19  GraphicsServices                    0x030b80d1 GSEventRun + 115
    20  UIKit                               0x002e9af2 UIApplicationMain + 1160
    21  NeighborMe                          0x000022f8 main + 102
    22  NeighborMe                          0x00002289 start + 53
    23  ???                                 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.

最佳答案

堆栈跟踪可以准确识别您的错误所在。它在-connectionDidFinishLoading里面,在线

[text appendFormat:@"%@\n", [address objectAtIndex:i]];

真正的罪魁祸首有点早:

NSArray *address = [responseString JSONValue];

您假设 JSON 表示一个数组,而实际上它包含一个字典。因此,您随后调用 -objectAtIndex: 会触发异常,因为 NSDictionary 不会响应该方法。

关于iphone - 应用程序因委托(delegate)而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4812470/

相关文章:

iPhone SDK - 大 UITableViewCell

iphone - 如何以编程方式在 UITableView 后面添加静态背景?

javascript - parse.coms javascript 库是否迭代 NSArray

iphone - iOS - 动态标签大小未正确返回

iphone - 在不缩放叠加层的情况下缩放背景图像

iphone - 如何在 Core Plot Y 轴上显示整数而不是小数?

iphone - 向 MP4 视频添加效果?

iphone - 解除分配后的 CATextLayer 内存残留

iphone - 是否可以保留“应用程序报告”用户位置?

ios - 解除模态视图 Controller 时如何保持呈现 View Controller 的方向?