gpt4 book ai didi

iOS - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序

转载 作者:行者123 更新时间:2023-11-28 22:40:38 29 4
gpt4 key购买 nike

所以我在 iOS 开发方面真的很新,我一直在尽我最大的努力,寻找答案,调试它以及我能想到的任何东西。

不过,我还没有找到解决问题的方法。

我一直在尝试获取一个外部 JSON 文档,它工作正常,但在解析它时,它会出错。

首先,这是我收到的所有错误消息。

2013-01-31 22:40:19.261 demodh[6205:c07] View Loaded
2013-01-31 22:40:19.479 demodh[6205:c07] -[__NSCFStringcountByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90
2013-01-31 22:40:19.480 demodh[6205:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x28d3 0xbd6589 0xbd4652 0xbd589a 0xbd460d 0xbd4785 0xb21a68 0x4615911 0x4614bb3 0x4652cda 0x1c358fd 0x465335c 0x46532d5 0x453d250 0x1c16f3f 0x1c1696f 0x1c39734 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x1d6d 0x1c95 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)

这是我目前使用的代码:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];

for(NSDictionary *dict in allDataDictionary)
{
if (![allDataDictionary isKindOfClass:[NSDictionary class]])
{
NSLog(@"2Unable to process temp array because it's an instance of %@", [allDataDictionary class]);
}
else
{
for(NSDictionary *deal in dict)
{
if (![deal isKindOfClass:[NSDictionary class]])
{
NSLog(@"Unable to process temp array because it's an instance of %@", [deal class]);
}
else
{
NSString *title = [deal objectForKey:@"title"];
NSLog(title);
}
}
}

}
}

我正在加载的 JSON 是:Link

希望您能帮助我找到解决方案。

最佳答案

问题是您正在快速枚举一个 NSDictionary 并期望获得它的值,而实际上您将获得它的键。当您尝试快速枚举一个 NSString 时,您会得到您所看到的断言。你可能想要这个:

for(NSObject *key in allDataDictionary) {
NSDictionary *dict = allDataDictionary[key];
...
for (NSObject *dealKey in dict) {
NSDictionary *deal = dict[dealKey];
}
...

或者,如果您真的想枚举值并且不需要键:

for(NSDictionary *dict in [allDataDictionary allValues]) {
...

关于iOS - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14635958/

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