[英]app crashes because of delegate
I have the following code in one of my .m class, 我的一个.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;
}
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 它在
-connectionDidFinishLoading
内部,在线
[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. 您假设JSON表示一个数组,而实际上它包含一个字典。 So your subsequent call of
-objectAtIndex:
triggers an exception, as NSDictionary does not respond to that method. 因此,您随后对
-objectAtIndex:
调用将触发异常,因为NSDictionary不响应该方法。
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]]; 您是否调试了代码并发现导致崩溃的行,因为我认为它在该行崩溃: [text appendFormat:@“%@ \\ n”,[address objectAtIndex:i]]; The logs says that unrecognized selector sent to NSCFDictionary.
日志显示无法识别的选择器已发送到NSCFDictionary。 So may be the above line is the cause and not the delegate.
因此,可能上述原因是原因而不是委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.