What is the visibility of @synthesized instance variables?

后端 未结 5 1285
刺人心
刺人心 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:22

    Other classes have access to everything that they #include. In other words, to everything that is inside your header.

    If something appears only in your implementation file, other classes (including subclasses) don't know it exists. A synthesized property is like that. Other classes know only about the property (a property means a getter and a setter method) but they don't know anything about the inner implementation of its methods.

    Note, that the access specifiers (public/private/protected) in obj-c are only a hint to the compiler that even if something appears in the header file, it can't be accessed. The runtime does not check it in any way.

    What happens if you put it into a class extension? Note that a property is a set of two methods. You just hide the methods from every class which includes your class main header but not the class extension header.

    We use this for example to declare a property as readonly and in class continuation we declare it as readwrite. Then, we can use the setter only from inside of the class.

提交回复
热议问题