سؤال

Suppose dummy VC has a private property "image"

dummyVC.h

@interface dummyVC : UIViewController
@end

dummyVC.m

@interface dummyVC ()
@property (nonatomic, strong) UIImage *image;
@end


- (void)setImage:(UIImage *)image
{
    if(!_image) { // compiler tells me _image is a 'undeclared indentifier'
        //do something
    }
}

If I try to use _image in its setter, the compiler tells me I am using undeclared identifier.

However, if I change the property name to image1 or anotherImage. There is no problem using _image1 and _anotherImage.

Can anybody explain why this happened?

هل كانت مفيدة؟

المحلول

I'm going to guess that the problem is either

  1. You are saying

    @synthesize image;
    

    without telling us about it, or

  2. you are supplying a getter method image without telling us about it.

When you say do either of those things and you supply a setter as you are doing here (setImage:), then the synthesized instance variable is named image, not _image. But if you do not supply both a setter and a getter, or you do not say @synthesize, the synthesized instance variable is named _image.

I'm betting that this is the difference between your cases.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top