gpt4 book ai didi

ios - 在 "for"循环中使用 NSURL URLWithString 时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 19:40:07 26 4
gpt4 key购买 nike

这是我的代码,

for (int i=0; i<=[selfLinksArray count]; i++) {
NSString *temp = selfLinksArray[i];
NSURL *tempURL = [NSURL URLWithString:temp ];
NSLog(@"NSURL:%@",tempURL);
NSData *tempData = [NSData dataWithContentsOfURL:tempURL];
NSError *error = nil;
NSDictionary *tempDict = [NSJSONSerialization JSONObjectWithData:tempData options:0 error:&error];
NSDictionary *volumeInfoDict = [tempDict objectForKey:@"volumeInfo"];
titleArray[i]=[volumeInfoDict objectForKey:@"title"];
}

Xcode 显示这个错误

[__NSArrayI length]: unrecognized selector sent to instance 0x7fdf334f0bc0
2016-01-19 16:12:34.937 BookFindr[7775:449799] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7fdf334f0bc0'
*** First throw call stack:
(

0 CoreFoundation 0x000000010f34be65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010edc4deb objc_exception_throw + 48
2 CoreFoundation 0x000000010f35448d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010f2a190a ___forwarding___ + 970
4 CoreFoundation 0x000000010f2a14b8 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x000000010f32e261 _CFURLCreateWithURLString + 81
6 Foundation 0x000000010e97a465 -[NSURL(NSURL) initWithString:relativeToURL:] + 349
7 Foundation 0x000000010e97a2e2 +[NSURL(NSURL) URLWithString:relativeToURL:] + 59
8 BookFindr 0x000000010e8bf9ab -[tabResTableViewController viewDidLoad] + 219
9 UIKit 0x000000010f88ef98 -[UIViewController loadViewIfRequired] + 1198
10 UIKit 0x000000010f894f4f -[UIViewController __viewWillAppear:] + 120
11 UIKit 0x000000010f8c4e44 -[UINavigationController _startCustomTransition:] + 1203
12 UIKit 0x000000010f8d523f -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
13 UIKit 0x000000010f8d63af -[UINavigationController __viewWillLayoutSubviews] + 57
14 UIKit 0x000000010fa7cff7 -[UILayoutContainerView layoutSubviews] + 248
15 UIKit 0x000000010f7af4a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
16 QuartzCore 0x000000011327b59a -[CALayer layoutSublayers] + 146
17 QuartzCore 0x000000011326fe70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
18 QuartzCore 0x000000011326fcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
19 QuartzCore 0x0000000113264475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
20 QuartzCore 0x0000000113291c0a _ZN2CA11Transaction6commitEv + 486
21 UIKit 0x000000010f6f2f7c _UIApplicationHandleEventQueue + 7329
22 CoreFoundation 0x000000010f277a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
23 CoreFoundation 0x000000010f26d95c __CFRunLoopDoSources0 + 556
24 CoreFoundation 0x000000010f26ce13 __CFRunLoopRun + 867
25 CoreFoundation 0x000000010f26c828 CFRunLoopRunSpecific + 488
26 GraphicsServices 0x0000000112b08ad2 GSEventRunModal + 161
27 UIKit 0x000000010f6f8610 UIApplicationMain + 171
28 BookFindr 0x000000010e8c101f main + 111
29 libdyld.dylib 0x0000000111a8792d start + 1
30 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

我已经从另一个 viewController 导入了 selfLinkArray。我想获取 VolumeInfo 字典中存在的各种标题。该字典存在于 selfLinksArray 中的每个单独链接中。请帮我解决这个错误。提前致谢。

最佳答案

让我们分析您的错误和回溯:

-[__NSArrayI length]: unrecognized selector sent to instance 0x7fdf334f0bc0'

问题写在这里:length 方法在 NSArray 对象上被调用。等等,这个示例中甚至没有写这个方法!别着急,我们来分析一下。

6 Foundation 0x000000010e97a465 -[NSURL(NSURL) initWithString:relativeToURL:] + 349

7 Foundation 0x000000010e97a2e2 +[NSURL(NSURL) URLWithString:relativeToURL:]

崩溃日志表明当您尝试初始化 NSURL 对象时发生崩溃。这个 init 方法的唯一参数是一个 NSString 对象。但是我们确定 temp 真的是一个 NSString 对象吗?

这个变量在没有检查它是否真的是一个 NSString 对象的情况下被转换。它可以是一个东西,例如...... NSArray 接收对 length 方法的调用。此外,此方法适用于 NSString 对象,因此 unrecognizer selector 错误看起来完全相关。

我们在哪里调用此 NSString 对象的 length?我的猜测是,根据回溯,URLWithString: 调用此方法来检查 NSString 长度,以便正确初始化 NSURL 对象。

关于ios - 在 "for"循环中使用 NSURL URLWithString 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34887078/

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