2013-08-22 20 views
0

我有以下代码:IOS是有可能得到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!!! 
+0

它属于codereview.stackexchange.com – Sauvage

回答

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 pr operty。

+0

因此,我们可以删除不必要的条件,但当前类不必知道基类。 – Sauvage

相关问题