2013-02-26 4 views
0

그래서 저는 코코아로 응용 프로그램을 작성 중이며 조금 문제가 있습니다. 배열 컨트롤러를 사용하여 많은 다른 폴더에있는 텍스트 파일 이름을 tableview로 보내고 싶지만 경계 오류가 계속 발생합니다. 나에게 문제가되는 코드 덩어리가있다.addObject에서 NSMutableDictionary에 범위 외 오류가 발생했습니다.

[self clear:nil]; // method that clears the NSTableView 

NSString *chooserSelection = chooserItems.titleOfSelectedItem; // gets the selected folder from an NSPopUpButton 

if (chooserSelection == @"All") { 
    path = @"/Library/Application Support/File Folder/Files/"; 
    NSArray *folders = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; 
    NSLog(@"\nfolders = %@", folders); 

    for (int i = 0; i < [folders count]; i++) { 

      path = @"/Library/Application Support/File Folder/Files/"; 
      NSMutableString *folderNames = [[NSMutableString stringWithString:path] init]; 
      [folderNames insertString:folders[i] atIndex:folderNames.length]; 
      [folderNames insertString:@"/" atIndex:folderNames.length]; 

      NSArray *text = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderNames error:nil]; 


     [myArrayController addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:text[i], @"File Name", nil]]; 
     //This line gives the error 

    } 
} 

[폴더 수]를 1로 변경하면 NSTableView에 표시됩니다. 그러나 아무것도 지나면 (for 루프를 통해 추가 전달) 나는 한계 오류가 발생합니다.

2013-02-26 13:17:49.679 AppName[3489:403] *** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0] 
2013-02-26 13:17:49.683 AppName[3489:403] (
    0 CoreFoundation      0x00007fff829b5f56 __exceptionPreprocess + 198 
    1 libobjc.A.dylib      0x00007fff84fe6d5e objc_exception_throw + 43 
    2 CoreFoundation      0x00007fff8295f370 -[__NSArrayI objectAtIndex:] + 208 
    3 AppName        0x000000010000159a -[AppDelegate tableView:objectValueForTableColumn:row:] + 106 
    4 AppKit        0x00007fff840a16e7 -[NSTableView _dataSourceValueForColumn:row:] + 73 
    5 AppKit        0x00007fff840a1167 -[NSTableView preparedCellAtColumn:row:] + 437 
    6 AppKit        0x00007fff840b2171 -[NSTableView _drawContentsAtRow:column:withCellFrame:] + 42 
    7 AppKit        0x00007fff840b05d8 -[NSTableView drawRow:clipRect:] + 1647 
    8 AppKit        0x00007fff840afdb5 -[NSTableView drawRowIndexes:clipRect:] + 565 
    9 AppKit        0x00007fff840aded7 -[NSTableView drawRect:] + 1390 
    10 AppKit        0x00007fff84197382 -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inContext:topView:] + 1435 
    11 AppKit        0x00007fff841b564f __-[NSView(NSLayerKitGlue) drawLayer:inContext:]_block_invoke_1 + 94 
    12 AppKit        0x00007fff841b52fb -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] + 2001 
    13 QuartzCore       0x00007fff82c4a0dc -[CALayer drawInContext:] + 145 
    14 QuartzCore       0x00007fff82c49225 CABackingStoreUpdate_ + 3221 
    15 QuartzCore       0x00007fff82c4813a _ZN2CA5Layer8display_Ev + 1086 
    16 AppKit        0x00007fff841504e6 -[NSView(NSInternal) _recursiveDisplayViewsIntoLayersIfNeeded] + 163 
    17 AppKit        0x00007fff841505fc -[NSView(NSInternal) _recursiveDisplayViewsIntoLayersIfNeeded] + 441 
    18 AppKit        0x00007fff841505fc -[NSView(NSInternal) _recursiveDisplayViewsIntoLayersIfNeeded] + 441 
    19 AppKit        0x00007fff841505fc -[NSView(NSInternal) _recursiveDisplayViewsIntoLayersIfNeeded] + 441 
    20 AppKit        0x00007fff84150168 -[NSView(NSLayerKitGlue) _drawRectAsLayerTree:] + 128 
    21 AppKit        0x00007fff8401653e -[NSView _drawRect:clip:] + 4382 
    22 AppKit        0x00007fff8401406d -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 3020 
    23 AppKit        0x00007fff84014b5e -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 5821 
    24 AppKit        0x00007fff84012da3 -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 270 
    25 AppKit        0x00007fff8400e1bb -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 4755 
    26 AppKit        0x00007fff84006c35 -[NSView displayIfNeeded] + 1676 
    27 AppKit        0x00007fff84006375 _handleWindowNeedsDisplayOrLayoutOrUpdateConstraints + 648 
    28 CoreFoundation      0x00007fff829758e7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    29 CoreFoundation      0x00007fff82975846 __CFRunLoopDoObservers + 374 
    30 CoreFoundation      0x00007fff8294aaf9 __CFRunLoopRun + 825 
    31 CoreFoundation      0x00007fff8294a486 CFRunLoopRunSpecific + 230 
    32 AppKit        0x00007fff84459b08 -[NSMenu _internalPerformActionForItemAtIndex:] + 38 
    33 AppKit        0x00007fff842e86f9 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 138 
    34 AppKit        0x00007fff841349bb NSSLMMenuEventHandler + 339 
    35 HIToolbox       0x00007fff89d08234 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1263 
    36 HIToolbox       0x00007fff89d07840 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 446 
    37 HIToolbox       0x00007fff89d1e6db SendEventToEventTarget + 76 
    38 HIToolbox       0x00007fff89d647b5 _ZL18SendHICommandEventjPK9HICommandjjhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 398 
    39 HIToolbox       0x00007fff89e4be2d SendMenuCommandWithContextAndModifiers + 56 
    40 HIToolbox       0x00007fff89e935b5 SendMenuItemSelectedEvent + 253 
    41 HIToolbox       0x00007fff89d5d883 _ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2_ + 101 
    42 HIToolbox       0x00007fff89e8bfed _ZL19PopUpMenuSelectCoreP8MenuData5PointdS1_tjPK4RecttjS4_S4_PK10__CFStringPP13OpaqueMenuRefPt + 1660 
    43 HIToolbox       0x00007fff89e8c2ac _HandlePopUpMenuSelection7 + 621 
    44 AppKit        0x00007fff842eb3c5 _NSSLMPopUpCarbonMenu3 + 3860 
    45 AppKit        0x00007fff846990fa _NSPopUpCarbonMenu3 + 39 
    46 AppKit        0x00007fff842e952a -[NSCarbonMenuImpl popUpMenu:atLocation:width:forView:withSelectedItem:withFont:withFlags:withOptions:] + 322 
    47 AppKit        0x00007fff844ca1f1 -[NSPopUpButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 564 
    48 AppKit        0x00007fff840cb7f6 -[NSControl mouseDown:] + 786 
    49 AppKit        0x00007fff84096c98 -[NSWindow sendEvent:] + 6306 
    50 AppKit        0x00007fff840303a5 -[NSApplication sendEvent:] + 5593 
    51 AppKit        0x00007fff83fc6a0e -[NSApplication run] + 555 
    52 AppKit        0x00007fff84242eac NSApplicationMain + 867 
    53 AppName        0x0000000100001152 main + 34 
    54 AppName        0x0000000100001124 start + 52 
) 

for 루프 아래 줄에 addObject 줄을 추가하여 마지막 파일의 파일 이름을 표시 할 수도 있습니다. 하지만 경계 밖 오류가 어디에서 왔는지 파악할 수 없습니다.

답변

0

먼저 경로의 폴더 수와 그 각각의 폴더에있는 파일 수에 대해 색인 i를 사용하고 있습니다. 그래서 당신은 적어도 의 숫자가 N 인 많은 파일들이 있다는 것에 의존하고 있습니다. 세 폴더

다음

FOLDER0 폴더 1 Folder2 폴더

당신이 Folder2 폴더 반복 (I = 2)가 예를 들어

, 거기에 있었다 더 나은 당신이 같은 폴더에 적어도 3 개 항목 수 항목 3 (텍스트 [2])에 액세스합니다. 항목 수가 적 으면 범위를 벗어난 오류가 발생합니다.

당신은 아마 이런 식으로 뭔가를 의미 : 당신에게 사전의 배열을 얻을 것이다

 NSMutableDictionary* dict = 
    [NSMutableDictionary dictionaryWithObjectsAndKeys:text, folders[i], nil]; 

합니다. 각 사전에는 단일 키 (폴더 이름)와 단일 값 (파일 이름 배열)이 있습니다.

배열 내부가 아닌 단일 사전에 데이터 구조를 병합하여 데이터 구조를 단순화 할 수 있습니다. 각 키는 폴더 이름이고, 각 값은 파일 이름의 배열입니다.

+0

감사! 알았어. –

관련 문제