自定义 NSMangedObject 访问器崩溃 NSOutlineView

Posted

技术标签:

【中文标题】自定义 NSMangedObject 访问器崩溃 NSOutlineView【英文标题】:Custom NSMangedObject accessor crashed NSOutlineView 【发布时间】:2012-10-04 19:08:12 【问题描述】:

我认为这是 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 类。

除了错误之外,谁能给我一个崩溃的原因?

【问题讨论】:

它没有因为行 returnObject = media.path; 而崩溃但它确实与行 returnObject = [media.path lastPathComponent]; 【参考方案1】:

我通过改变方法解决了这个问题

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

【讨论】:

以上是关于自定义 NSMangedObject 访问器崩溃 NSOutlineView的主要内容,如果未能解决你的问题,请参考以下文章

自定义表视图控制器因“initWithCoder:无法识别的选择器发送到实例”而崩溃

从自定义框架访问 Coredata 时应用程序崩溃

Swift 3.1:自定义错误转换为 NSError 以访问其域属性时崩溃

自定义 CollectionView 崩溃 iOS

Qt Designer 使用自定义插件崩溃

具有多个 UIButton 的多个自定义单元格崩溃