2013-10-20 38 views
0

我目前有一個數組超出界的問題,無法找到一個方法如何解決它。我從我的崩潰報告中懷疑我對這部分代碼有問題。任何建議,我應該如何解決這個問題?隨意問一個特定的代碼。陣列超出界限崩潰

-(void)viewDidLoad 
{ 
    NSLog(@"a"); 
    [super viewDidLoad];  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    //configure carousel1 
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:@"Tops"]; 
    NSArray *directoryContent = [fileManager directoryContentsAtPath: fPath]; 
    imageArray1 = [directoryContent mutableCopy]; 
    NSLog(@"%@", [imageArray1 objectAtIndex:0]);←This line is 70 
    NSLog(@"1:::%d", [imageArray1 count]); 

    //configure carousel2 
    fPath = [documentsDirectory stringByAppendingPathComponent:@"Bottoms"]; 
    directoryContent = [fileManager directoryContentsAtPath:fPath]; 
    NSLog(@"%@", fPath); 
    imageArray2 = [directoryContent mutableCopy]; 
    NSLog(@"2:::%d", [imageArray2 count]); 

    carousel1.type = iCarouselTypeLinear; 
    carousel2.type = iCarouselTypeLinear; 

    [carousel1 reloadData]; 
    [carousel2 reloadData]; 

    carousel1.delegate = self; 
    carousel1.dataSource = self; 
    carousel2.delegate = self; 
    carousel2.dataSource = self; 
} 

更新

Exception Type: EXC_CRASH (SIGABRT) 
Exception Codes: 0x0000000000000000, 0x0000000000000000 
Triggered by Thread: 0 

Last Exception Backtrace: 
0 CoreFoundation     0x2edecf4e __exceptionPreprocess + 126 
1 libobjc.A.dylib     0x390986aa objc_exception_throw + 34 
2 CoreFoundation     0x2ed235ee -[__NSArrayM objectAtIndex:] + 226 
3 Appname       0x000acab8 -[iCarouselExampleViewController viewDidLoad] (iCarouselExampleViewController.m:70) 
4 UIKit       0x315625fe -[UIViewController loadViewIfRequired] + 514 
5 UIKit       0x315623bc -[UIViewController view] + 20 
6 UIKit       0x3160c632 -[UIViewController shouldAutorotate] + 22 
7 UIKit       0x31648d76 -[UIViewController _preferredInterfaceOrientationForPresentationInWindow:fromInterfaceOrientation:] + 246 
8 UIKit       0x316476ae -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:] + 2054 
9 UIKit       0x31646700 -[UIViewController presentViewController:withTransition:completion:] + 4664 
10 UIKit       0x31821656 -[UIViewController presentModalViewController:animated:] + 26 
11 UIKit       0x31592f3a -[UIApplication sendAction:to:from:forEvent:] + 86 
12 UIKit       0x31592eda -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 26 
13 UIKit       0x31592eb4 -[UIControl sendAction:to:forEvent:] + 40 
14 UIKit       0x3157eb3a -[UIControl _sendActionsForEvents:withEvent:] + 370 
15 UIKit       0x3159292a -[UIControl touchesEnded:withEvent:] + 586 
16 UIKit       0x315925fc -[UIWindow _sendTouchesForEvent:] + 524 
17 UIKit       0x3158d688 -[UIWindow sendEvent:] + 828 
18 UIKit       0x31562a20 -[UIApplication sendEvent:] + 192 
+1

請在上面的代碼中添加崩潰報告,錯誤信息並顯示它指向哪一行。 – Kerni

+0

我已經添加崩潰報告 – interdatega

回答

0

如果我冒險猜測,目錄是空的。

imageArray1 = [directoryContent mutableCopy]; 
NSLog(@"%@", [imageArray1 objectAtIndex:0]);←This line is 70 

如果imageArray1沒有內容,第70行將崩潰,就像您的報告顯示的那樣。除了崩潰之外,您應該檢查[imageArray1 count]並且只有在有值時才使用它。你的代碼在這裏並沒有對這些數組做任何事情,但是這裏應該是這樣的結構。你還需要爲carousel2做同樣的事情。

//configure carousel1 
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:@"Tops"]; 
NSArray *directoryContent = [fileManager directoryContentsAtPath: fPath]; 
imageArray1 = [directoryContent mutableCopy]; 
if ([imageArray1 count] > 0) { 
    NSLog(@"%@", [imageArray1 objectAtIndex:0]); //This line is 70 
    NSLog(@"1:::%d", [imageArray1 count]); 
} else { 
    NSLog(@"No objects in imageArray1."); 
} 
+0

謝謝,可能是它,我應該如何解決它?任何建議? – interdatega

+0

@interdatega我爲你更新了答案。 – Holly

0

您不同意的日誌,這將有助於,也沒有你的委託方法,這將有助於輸出。也就是說,在設置數據源和委託之後,您應該發送reloadData作爲最後一條消息 - 這是一種很好的做法(最近iOS似乎將操作推遲到運行循環結束時)。