Objective c 执行NSMutableArray后获取错误
这是代码。我试图创建一个包含名称、大小和图像(从资源获取)的对象,然后将该对象添加到Objective c 执行NSMutableArray后获取错误,objective-c,macos,cocoa,nsmutablearray,Objective C,Macos,Cocoa,Nsmutablearray,这是代码。我试图创建一个包含名称、大小和图像(从资源获取)的对象,然后将该对象添加到NSMutableArray中,以便在NSTableView中显示这些对象。然而,在我运行代码之后,我确实在NSMutableArray中获得了正确的对象计数,并在每个对象中获得了正确的信息(名称、大小、图像),但它仍然会给我错误信息。你们能帮帮我吗。我是一个新的Mac开发者。谢谢 代码如下: @implementation AppDelegate{ NSMutableArray* _ta
NSMutableArray
中,以便在NSTableView中显示这些对象。然而,在我运行代码之后,我确实在NSMutableArray
中获得了正确的对象计数,并在每个对象中获得了正确的信息(名称、大小、图像),但它仍然会给我错误信息。你们能帮帮我吗。我是一个新的Mac开发者。谢谢
代码如下:
@implementation AppDelegate{
NSMutableArray* _tableContents;
}
-(void)awakeFromNib{
NSString* imagePath = @"USB.png";
NSString* mountPath = @"/Volumes";
_tableContents = [[NSMutableArray alloc]init];
for(mountPath in [[NSWorkspace sharedWorkspace]mountedLocalVolumePaths])
{
if(0==[mountPath rangeOfString:@"/Volumes/"].location)
{
NSString* USBpath = [[[NSWorkspace sharedWorkspace]mountedLocalVolumePaths]objectAtIndex:count];
//get the name of USB drive
NSString* nameOfUSB = [USBpath substringFromIndex:9];
//get the size of USB drive
NSNumber *volumeSize;
NSURL *mountPathPicked = [NSURL fileURLWithPath:mountPath];
if([mountPathPicked getResourceValue:&volumeSize forKey:NSURLVolumeTotalCapacityKey error:nil])
{
NSDictionary *obj = @{@"name":nameOfUSB,
@"image":[NSImage imageNamed:imagePath],
@"size":volumeSize
};
//after I call the following line, it will give me error !!
[_tableContents addObject:obj];
}
}
count ++;
}
}
//I also use the following two methods in order to show the name, and image in NSTableView
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return [_tableCountents count];
}
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSDictionary *flag = _tableContents[row];
NSString *identifier = [tableColumn identifier];//call different col we need identifier
if([identifier isEqualToString:@"MainCell"])
{
NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"MainCell" owner:self];//give back specific cell named mainCell
[cellView.textField setStringValue:flag[@"name"]];
[cellView.imageView setImage:flag[@"image"]];
return cellView;
}
return nil;
}
GUI中没有连接问题。下面是错误消息:
2013-04-25 01:16:41.321 NAMEOFPROJECT[8243:303] An uncaught exception was raised
2013-04-25 01:16:41.322 NAMEOFPROJECT[8243:303] *** -[__NSArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 3]
2013-04-25 01:16:41.329 NAMEOFPROJECT[8243:303] (
0 CoreFoundation 0x00007fff9147cb06 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff900c03f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff9142fb53 -[__NSArrayI objectAtIndex:] + 163
3 CDTOANYDRIVE 0x00000001000011d6 -[AppDelegate DetectingUSBDrives] + 710
4 CDTOANYDRIVE 0x0000000100000efb -[AppDelegate awakeFromNib] + 43
5 AppKit 0x00007fff899631a8 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1234
6 AppKit 0x00007fff89b79976 -[NSNib _instantiateNibWithExternalNameTable:] + 610
7 AppKit 0x00007fff89b7a41a -[NSNib instantiateNibWithExternalNameTable:] + 17
8 AppKit 0x00007fff89b262b4 -[NSTableRowData _unarchiveViewWithIdentifier:owner:] + 520
9 CDTOANYDRIVE 0x000000010000174b -[AppDelegate tableView:viewForTableColumn:row:] + 219
10 AppKit 0x00007fff89b279a2 -[NSTableRowData _addViewToRowView:atColumn:row:] + 324
11 AppKit 0x00007fff89b2766f -[NSTableRowData _addViewsToRowView:atRow:] + 151
12 AppKit 0x00007fff89b25c05 -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 415
13 AppKit 0x00007fff89b2598a -[NSTableRowData _addRowViewForVisibleRow:withPriorRowIndex:inDictionary:withRowAnimation:] + 272
14 AppKit 0x00007fff89b24c59 -[NSTableRowData _unsafeUpdateVisibleRowEntries] + 740
15 AppKit 0x00007fff89b247f1 -[NSTableRowData updateVisibleRowViews] + 119
16 AppKit 0x00007fff89afc5d7 -[NSTableView layout] + 165
17 AppKit 0x00007fff89aafe95 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 112
18 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
19 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
20 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
21 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
22 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
23 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
24 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
25 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
26 AppKit 0x00007fff89aafd2e -[NSView layoutSubtreeIfNeeded] + 615
27 AppKit 0x00007fff89aab4dc -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 201
28 AppKit 0x00007fff899d96a8 -[NSView _layoutAtWindowLevelIfNeeded] + 99
29 AppKit 0x00007fff899d8fe6 -[NSView _sendViewWillDrawInRect:clipRootView:] + 87
30 AppKit 0x00007fff899a59b1 -[NSView displayIfNeeded] + 1044
31 AppKit 0x00007fff89a62a48 -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1377
32 AppKit 0x00007fff89a62068 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 940
33 AppKit 0x00007fff89a61c4f -[NSWindow orderWindow:relativeTo:] + 159
34 AppKit 0x00007fff89963266 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1424
35 AppKit 0x00007fff8994214d loadNib + 317
36 AppKit 0x00007fff89941679 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 219
37 AppKit 0x00007fff899414ae -[NSBundle(NSNibLoading) loadNibNamed:owner:topLevelObjects:] + 200
38 AppKit 0x00007fff8994128e +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 360
39 AppKit 0x00007fff8993da2f NSApplicationMain + 398
40 CDTOANYDRIVE 0x0000000100000e82 main + 34
41 libdyld.dylib 0x00007fff9385a7e1 start + 0
42 ??? 0x0000000000000003 0x0 + 3
)
以上错误显示两次 我在一个迷你应用程序中试用了你的代码,效果很好。有几件事我可以强调:
count
作为索引。这个变量是否初始化为零init
方法我在一个迷你应用程序中试用了你的代码,效果很好。有几件事我可以强调:
count
作为索引。这个变量是否初始化为零init
方法此问题与iPhone、iOS主题无关。@YU FENG您可以使用NSlog语句调试行和[_TableCountmentsCount]来检查是否一切正常希望您在tableContent填充后调用reloadData,此问题与iPhone、iOS主题无关。@YU FENG您可以使用NSlog语句调试行和行[\u tableCountents count]要检查是否一切正常,请在tableContent填充后调用reloadData,谢谢。我发现了问题。你是对的,我应该将所有内容移到init方法。再次感谢!!谢谢,我发现了问题。你是对的,我应该将所有内容移到init方法。再次感谢!!