为什么必须在'.class-property可读之前初始化NSObject

时间:2016-08-21 06:59:14

标签: ios objective-c uiimageview

如果我这样说:

127.0.0.1

它会按预期记录UIImageView *imageView; imageView = [[UIImageView alloc] init]; NSLog(@"%@", imageView.class);

现在,如果我以一种方式编写代码,其中UIImageView的UIImageView属性用于分配对象(在第0行声明/定义为UIImageView),它将记录{{1} }

.class

即使我在阅读其“(null)值时将对象强制转换为UIImageView,但是当我稍后再记录所述类值时,我仍然得到UIImageView *myImageView; myImageView = [[myImageView.class alloc] init]; NSLog(@"%@", myImageView.class);//logs (null)

.class

1 个答案:

答案 0 :(得分:2)

因为您尚未初始化myImageView.class会向您nil发送UIImageView *myImageView; myImageView = [[myImageView.class alloc] init]; // didn't initialize myImageView NSLog(@"%@", myImageView.class);//logs (null)

myImageView = [[NSClassFromString(@"UIImage") alloc] init];

NSLog(@"%@", myImageView.class);//logs (UIImage)

所以,如果你想做那样的事情:

complete(Future[Option[T]])