gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:08:04 25 4
gpt4 key购买 nike

我的一个 .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com