应用程序在“for”循环

时间:2016-01-19 21:18:05

标签: ios xcode for-loop nsurl

这是我的代码,

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中的每个单独链接中。请帮我解决这个错误。在此先感谢。

1 个答案:

答案 0 :(得分:3)

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

  

- [__ NSArrayI length]:无法识别的选择器发送到实例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错误看起来完全相关。

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