2014-01-13 44 views
1

這基本上是帶有數據源的NSOutlineView的「hello world」。我所做的只是將NSOutlineView拖入MainMenu.xib窗口,將其連接到插座,然後嘗試將其數據源設置爲簡單實現。當我運行它時,我得到Thread 1: EXC_BAD_ACCESS,但調試器中的堆棧跟蹤幫助我。爲什麼在這個簡單的NSOutlineView數據源中EXC_BAD_ACCESS?

(lldb) bt 
* thread #1: tid = 0x158e35, 0x00007fff8948d097 libobjc.A.dylib`objc_msgSend + 23, queue = 'com.apple.main-thread, stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT) 
frame #0: 0x00007fff8948d097 libobjc.A.dylib`objc_msgSend + 23 
frame #1: 0x00007fff8839e590 AppKit`loadItemEntryLazyInfoIfNecessary + 120 
frame #2: 0x00007fff883d3e5f AppKit`-[NSOutlineView _rowEntryForRow:requiredRowEntryLoadMask:] + 77 
frame #3: 0x00007fff883d37d1 AppKit`-[NSOutlineView frameOfCellAtColumn:row:] + 323 
frame #4: 0x00007fff885d5548 AppKit`-[NSTableView drawRow:clipRect:] + 1154 
frame #5: 0x00007fff885d4f7d AppKit`-[NSTableView drawRowIndexes:clipRect:] + 776 
... 

當我設置斷點時,我只看到我的一個方法被調用。 (outlineView:numberOfChildrenOfItem:)這是我的代碼。有任何想法嗎?

它創建三個簡單的「根」項目,並返回值的字符串。根據斷點,無論如何,這絕不會被調用。

在應用程序的委託:

@implementation TmTrkAppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)notification 
{ 
    _outlineView.dataSource = [[TmTrkOutlineViewDataSource alloc] init]; 
} 

@end 

數據來源:

@interface TmTrkOutlineViewDataSource : NSObject <NSOutlineViewDataSource> 

@end 
... 

@interface TmTrkTagItem : NSObject { 

} 

@end 

@implementation TmTrkTagItem { 

} 

@end 

@implementation TmTrkOutlineViewDataSource { 
    NSMutableArray *_roots; 
} 

- (id)init { 
    printf("init\n"); 
    self = [super init]; 
    if (self) { 
     _roots = [[NSMutableArray alloc] init]; 
     for (int i=0; i<3; i++) { 
      id item = [TmTrkTagItem new]; 
      [_roots addObject:item]; 
     } 
     printf("Created _roots %d.\n", (int)_roots.count); 
    } 
    return self; 
} 

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item { 
    if (!item) { 
     printf("child: %d ofItem: %s\n", (int)index, [item description].UTF8String); 
     return _roots[index]; 
    } 
    return nil; 
} 

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item { 
    if (!item) return YES; 
    else return NO; 
} 

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { 
    if (!item) return _roots.count; 
    else return 0; 
} 

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { 
    NSString *val = @"Test Value"; 
    return val; 
} 

@end 

這是一個新的項目,在Xcode 5.0.2,啓用ARC。

+2

您的數據源可能會在您創建它之後立即發佈,因爲您的應用程序委託並未維護對它的強引用,並且NSOutlineView的dataSource也是一個弱引用。 – NSAdam

+0

Ugg。是的,就是這樣。謝謝。我在Java世界花了太長時間。 –

+0

@NSAdam請重新發布您的評論作爲答案,以便我們可以關閉此問題,您可以獲得信貸! –

回答

1

您的數據源可能會在您創建它之後立即發佈,因爲您的應用程序委託並未維護對它的強引用,並且NSOutlineView的dataSource也是一個弱引用。

相關問題