id object = [[Child alloc] init];// 类型转换Child *child = (Child *) object ;[child myMethod] ;
id类型所存储的对象到运行时才判断对象所属的类,当我们使用id类型调用一个不存在的方法时,运行之前并不报错,运行起来后程序会崩溃.比如下面这个错误的例子.
// animal 没有main方法,编译器并不报错[animal main] ;
-(id) initWithFactory : (NSInteger) sel {id objVal = nil ;if (sel == 0){objVal =[ [Cat alloc] init ];}else{objVal = [[Dog alloc] init];}return objVal ;}
原文:http://www.cnblogs.com/mrwu/p/4331152.html