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