0

我有以下代码:

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

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

frc - NSFetchedResultsController.

如何获取相同对象字段的数据AuthorLocation?类必须不知道 Author 和 Location 的存在。

我也尝试这样做:

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

1 回答 1

2

一种方法是制作这样LocationAuthor子类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);

访问该name属性。

于 2013-08-22T13:59:23.503 回答