【问题标题】:App crashes while using NSURL URLWithString in a "for" loop在“for”循环中使用 NSURL URLWithString 时应用程序崩溃
【发布时间】:2016-01-19 21:18:05
【问题描述】:

这是我的代码,

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 中的每个单独的链接中。请帮我解决这个错误。提前致谢。

【问题讨论】:

  • 您在某些时候没有得到您期望的字符串。您将数组作为循环中的元素。设置一个异常断点停止在违规行,您可以从那里检查原因。

标签: ios xcode for-loop nsurl


【解决方案1】:

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

-[__NSArrayI 长度]:无法识别的选择器发送到实例 0x7fdf334f0bc0'

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

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

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

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

这个变量被强制转换而不检查它是否真的是一个NSString 对象。它可以是 anthing,例如...NSArray 接收对 length 方法的调用。此外,NSString 对象存在此方法,因此 unrecognizer selector 错误看起来完全相关。

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

【讨论】:

    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2021-06-27
    相关资源
    最近更新 更多