开发者

change type from id to subclassed nsmanagedobject

I need this operation bsc method receive id and i like to using dot syntax to set object later. Currently i do by this way. But maybe somebody know more elegant way?

-(NSError *) updateObject:(id)object operation:(NSInteger)operation;
{
CurrentCompany *obj1 = nil;
 ...
CompanyStuff *obj2 = nil;


if ([[[(CurrentCompany *)object entity]开发者_StackOverflow name] isEqualToString:@"CurrentCompany"]) obj1 = (CurrentCompany *)object;
if ([[[(CompanyStuff *)object entity] name] isEqualToString:@"CompanyStuff"]) obj2 = (CompanyStuff *)object;

NSLog(@"UpdatedObject:%@",obj1);


If these classes conform to a common protocol or inherit from a common superclass that declares the properties, you can just statically type the variable as that protocol or superclass. If neither of these are the case, it doesn't seem like they should be treated interchangeably anyway.

Also, this isn't really related, but the explicit cast from id to the specific class is pointless. You can just assign.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜