IOS是否可以在不进行转换的情况下获取NSFetchedResultsController字段的数据

时间:2013-08-22 13:46:27

标签: ios casting nsfetchedresultscontroller

我有以下代码:

if ([[frc objectAtIndexPath:indexPath] isKindOfClass:[Author class]])
    return ((Author *)[frc objectAtIndexPath:indexPath]).name;

return ((Location *)[frc objectAtIndexPath:indexPath]).name;

frc - NSFetchedResultsController

如何获取对象AuthorLocation的相同字段的数据?班级必须不知道作者和位置的存在。

我也试过这样做:

id <NSFetchedResultsSectionInfo> sectionInfo = m_arts.sections[indexPath.section];
id object = sectionInfo.objects[indexPath.row];
NSLog(@"%@", object[@"name"]); // Crash!!!

1 个答案:

答案 0 :(得分:2)

一种方法是Location的{​​{1}}和Author子类如下:

NamedObject

然后像:

@interface NamedObject : NSObject
@property (strong) NSString *name;
@end

@interface Author : NamedObject
// ...
@end

@interface Location : NamedObject
// ...
@end

访问NamedObject *object = (NamedObject *) sectionInfo.objects[indexPath.row]; NSLog (@"%@", object.name); 属性。