1

Here is the code. I tried to create a object which includes the name, size and image(get from the resource) and then I add that object into NSMutableArray in order to show those object in NSTableView. However, after I run the code, I did get the correct counts of object in NSMutableArray and correct information in each object(name,size,image), but it still gives me error. Could you guys help me. I am a new Mac developer. Thanks !!

Here is the code:

    @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;
}

There is no connection problems in GUI. And here is the error message:

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
)

And the above error shows twice !!

3
  • This question is not related iPhone, iOS topic. Commented Apr 25, 2013 at 5:43
  • @YU FENG You can put NSlog statements to debug the row and the [_tableCountents count] to check if everything went right Commented Apr 25, 2013 at 5:57
  • hope you would be calling reloadData after tableContent gets filled, Commented Apr 25, 2013 at 6:11

1 Answer 1

1

I have tried your code in a mini-app and It works fine. There are few things that I might highlight:

  1. Your exception occurs in the method DetectingUSBDrives, for which we don't have any code. I'm assuming that you have pasted the code in the awakeFromNib method.
  2. You are using count as an index. Is this varibale initialized at zero?
  3. The fact that the error appears twice suggests that your cell views hold a reference to your app delegate, which means that awakeFromNib is called once for each view creation. This can create many problem since you are initializing _tableOfContents there. You might want to move all that code to the init method instead.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man. I found the problem. You are right I should move everything to init method instead. Thanks again !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.