What is the visibility of @synthesized instance variables?

后端 未结 5 1275
刺人心
刺人心 2020-11-28 10:33

If you have a property in your public interface like the following

@interface MyClass : NSObject
@property(strong) NSString *myProp;
@end

A

5条回答
  •  鱼传尺愫
    2020-11-28 11:21

    A synthesized variable acts as if declared @private:

    @interface Garble : NSObject
    @property (copy) NSString * s; 
    @end
    @implementation Garble
    @synthesize s;
    @end
    
    @interface Bargle : Garble
    @end
    
    @implementation Bargle
    
    - (void) useS {
        NSLog(@"%@", s);    // error: instance variable 's' is private
    }
    
    @end
    

    I swear I've seen this in the docs, but I can't find it right now. Will update if I track it down.

提交回复
热议问题