有没有办法在objective-c中投射对象?
例如,我有这个对象,并希望将其转换为类:
NSManagedObject *item = [array objectAtIndex:indexPath.row];
答案 0 :(得分:9)
我想你想这样:
if ([[[array objectAtIndex:indexPath.row] isKindOfClass:[NSManagedObject class]])
{
NSManagedObject *item = (NSManagedObject *)[array objectAtIndex:indexPath.row];
}
首先在Auto Generating Model Files
下查看this link,了解作者如何将NSManagedObject
更改为强类型对象。
那么剩下的就是:
if ([[[array objectAtIndex:indexPath.row] isKindOfClass:[Contact class]])
{
Contact *item = (Contact *)[array objectAtIndex:indexPath.row];
}
希望有所帮助!
答案 1 :(得分:1)
是的,只需做
NSManagedObject *item = (NSManagedObject *)[array objectAtIndex:indexPath.row];