开发者

app crashes because of delegate

I have the following code in one of my .m class,

- (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;
}

The app just closes down when running in the simulator, I am guessing it's because of the delegate... but I can see that everything is running fine. I got the following error at my console:

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”.


The stack trace identifies precisely where your error is. It's inside of -connectionDidFinishLoading, on the line

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

The real culprit is a bit earlier:

NSArray *address = [responseString JSONValue];

You're assuming that the JSON represents an array, when in fact it contains a dictionary. So your subsequent call of -objectAtIndex: triggers an exception, as NSDictionary does not respond to that method.


Did you debug the code and found which line is causing a crash, because i think it is crashing at the line: [text appendFormat:@"%@\n", [address objectAtIndex:i]]; The logs says that unrecognized selector sent to NSCFDictionary. So may be the above line is the cause and not the delegate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜