app由于委托而崩溃

时间:2011-01-27 03:35:08

标签: 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”.

2 个答案:

答案 0 :(得分:7)

堆栈跟踪精确识别错误的位置。它位于-connectionDidFinishLoading内,位于

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

真正的罪魁祸首是早一点:

NSArray *address = [responseString JSONValue];

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

答案 1 :(得分:1)

您是否调试了代码并找到导致崩溃的行,因为我认为它正在崩溃: [text appendFormat:@“%@ \ n”,[address objectAtIndex:i]]; 日志说无法识别的选择器发送到NSCFDictionary。因此,上述行可能是原因,而不是代表。