我有以下代码:
if ([[frc objectAtIndexPath:indexPath] isKindOfClass:[Author class]])
return ((Author *)[frc objectAtIndexPath:indexPath]).name;
return ((Location *)[frc objectAtIndexPath:indexPath]).name;
frc - NSFetchedResultsController
。
如何获取对象Author
和Location
的相同字段的数据?班级必须不知道作者和位置的存在。
我也试过这样做:
id <NSFetchedResultsSectionInfo> sectionInfo = m_arts.sections[indexPath.section];
id object = sectionInfo.objects[indexPath.row];
NSLog(@"%@", object[@"name"]); // Crash!!!
答案 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);
属性。