我认为这是 Core Data 中的一个错误,但在我提交错误报告之前,我想确定这不仅仅是我愚蠢。
我设置了一个 NSOutlineView 来访问 5 个不同的核心数据实体的数据。每个实体的数据都通过不同的 NSArrayController 访问,该 NSArrayController 绑定到实体及其“ManagedObjectContext”。然后我让 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 实体属性 fullName 和 Media 实体属性 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;
}
当我尝试扩展 Person 或 Media 实体时程序崩溃了,但当我扩展其他实体时却没有。我将崩溃追踪到 [NSCell _setContents:][NSObject(NSObject) doesNotRecognizeSelector:]
我将返回的 Media 属性更改为标准的 Core Data 访问器属性@“path”,当我展开 Media 实体时程序停止崩溃。所以问题肯定与自定义访问器有关。
仅供参考 - 我检查以确保实体设置为使用 NSManagedObject 类。
除了错误,谁能给我一个崩溃的原因?