0

我想這是一個核心數據中的錯誤,但在我提交錯誤報告之前,我想確定它不僅僅是我愚蠢。自定義NSMangedObject訪問器崩潰NSOutlineView

我設置了一個NSOutlineView來訪問5個不同的Core Data實體的數據。每個實體的數據都通過綁定到實體及其「ManagedObjectContext」的不同NSArrayController訪問。然後,根據擴展的實體,NSOutlineViewDataSource方法返回正確的NSString對象。

注意:實體在其他地方聲明爲具有實體名稱的NSArray。

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index 
     ofItem:(id)item { 

if(nil == item) { 
    return [entities objectAtIndex:index]; 
} 
NSInteger entityIdx = [entities indexOfObject:item]; 
if (entityIdx == NSNotFound) { 
    return @""; 
} 
id returnObject = @""; 
switch (entityIdx) { 
    case 0: { 
     Person *person = [[peopleArrayController arrangedObjects] objectAtIndex:index]; 
     returnObject = person.fullName; 
     break; 
    } 
    case 1: { 
     Media *media = [[mediaArrayController arrangedObjects] objectAtIndex:index]; 
     returnObject = media.imageTitle; 
     break; 
    } 
    case 2: { 
     Note *note = [[notesArrayController arrangedObjects] objectAtIndex:index]; 
     returnObject = note.noteDescription; 
     break; 
    } 
    case 3: { 
     Source *source = [[sourcesArrayController arrangedObjects] objectAtIndex:index]; 
     returnObject = source.title; 
     break; 
    } 
    case 4: { 
     Repository *repo = [[repostioriesArrayController arrangedObjects] objectAtIndex:index]; 
     returnObject = repo.name; 
     break; 
    } 
    default: 
     break; 
} 


return returnObject; 

}

Person實體屬性全名和媒體實體屬性imageTitle是定製的存取。

- (NSString *)fullName { 
[self willAccessValueForKey:@"surName"]; 
[self willAccessValueForKey:@"givenName"]; 
NSString *firstName = [self valueForKey:@"givenName"]; 
NSString *lastName = [self valueForKey:@"surName"]; 
NSString *string = [NSString stringWithFormat:@"%@ %@", (firstName) ? firstName : @"", (lastName) ? lastName : @""]; 
[self didAccessValueForKey:@"surName"]; 
[self didAccessValueForKey:@"givenName"]; 
return string; 

}

- (id) imageTitle { 
[self willAccessValueForKey:@"path"]; 
id title = [[self valueForKey:@"path"] lastPathComponent]; 
[self didAccessValueForKey:@"path"]; 
return title; 

}

的程序崩潰時,我試圖擴大人或媒體實體,但不是當我擴大了其他實體。我追蹤的崩潰爲[的NSCell _setContents:] [NSObject的(NSObject的)doesNotRecognizeSelector:]

我改變返回到標準的核心數據存取器屬性@「路徑」的媒體屬性和程序停止崩潰的時候我擴大了媒體實體。所以問題肯定與自定義訪問器有關。

僅供參考 - 我檢查確保實體設置爲使用NSManagedObject類。

任何人都可以給我一個錯誤的原因除了一個錯誤?

+0

它沒有與行returnObject = media.path;但它確實與行returnObject = [media.path lastPathComponent]; –

回答

0

我解決此問題得到了通過改變方法

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item

返回管理對象

case 1: { 
     Media *media = [[mediaArrayController arrangedObjects] objectAtIndex:index]; 
     returnObject = media; 
     break; 
    } 

然後,我有方法 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item

返回字符串

if ([item isKindOfClass:[Media class]]) { 
    Media *media = (Media *)item; 
    return media.imageTitle; 
}