神刀安全网

Issue of lazy loading property

I’ve got some readonly properties in my .h file. And when I tried to lazy loading them as usual like this.

// MyView.h @property (nonatomic, strong, readonly) UILabel *titleLabel;  // MyView.m - (UIImageView *)imageView {     if (!_imageView)     {         _imageView = [UIImageView new];         _imageView.translatesAutoresizingMaskIntoConstraints = NO;         _imageView.backgroundColor = [UIColor clearColor];         _imageView.contentMode = UIViewContentModeScaleAspectFit;         _imageView.userInteractionEnabled = NO;         _imageView.accessibilityIdentifier = @"empty set image view";          _imageView.image = _defaultImage;          [_contentView addSubview:_imageView];     }     return _imageView; } 

Then…bang! It threw out errors in bulk, yelling “Use of undeclared identifier ‘_imageView’”. What the hell have I done!

Luckily, I found some explainations here . It says:

Newer Xcode versions can create a @synthesize statement automatically and use the underscore prefix for instance variables. In this case however, since the property is read-only and you provide a getter method, Xcode does not synthesize the property automatically.

So, I have to @synthesize the property on my own, and now it’s quiet and cute again. 😛

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Issue of lazy loading property

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址