0

所以我正在用可可构建一个应用程序并且遇到了一些麻烦。我想使用数组控制器将许多不同文件夹中的文本文件名提供给 tableview,但它不断抛出越界错误。这是一大段代码给我带来了麻烦。

[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

    }
}

如果我将 [folders count] 更改为 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
)

我还可以将 addObject 行添加到 for 循环下面的行,这将显示最后一个文件的文件名。但我无法弄清楚越界错误来自哪里。

4

1 回答 1

0

您首先使用索引 i 来计算路径中的文件夹数,然后使用索引来计算相应文件夹中的文件数。因此,您取决于文件夹N中的文件至少与N的数量一样多。

例如,如果您有三个文件夹

文件夹0 文件夹1 文件夹2

那么当您遍历文件夹 2(i=2)时,文件夹中最好至少有 3 个项目,因为您正在访问项目 3(文本 [2])。当项目较少时,您将收到越界错误。

你可能打算做这样的事情:

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

这将为您提供一系列字典。每个字典都有一个键(文件夹的名称)和一个值(文件名数组)。

您可以通过将其展平为单个字典(不在数组内)来进一步简化数据结构。每个键是一个文件夹名称,每个值是文件名数组。

于 2013-02-26T20:17:12.313 回答