Are synthesized instance variables generated as private instead of protected?

后端 未结 2 536
长发绾君心
长发绾君心 2020-12-18 09:48

Since recent runtimes in iOS, we are able to define properties that will generate accessors for instance variables. From what I understand, it is not mandatory to declare th

相关标签:
2条回答
  • 2020-12-18 10:37

    Use:

    self.size = 10;
    

    That will map to setSize method.

    0 讨论(0)
  • 2020-12-18 10:44

    Any instance variable not declared in the main interface is automatically private, and this cannot be overridden. If you try to use a scope modifier when defining instance variables in the implementation, you will get an error that the specification is inconsistent.

    The reason for this is that there is usually only one class per implementation file, which means the compiler doesn't know about the instance variable when compiling other classes. If you have multiple classes in the same file, the compiler could know about it, but you still aren't allowed to override the scope. Possible reasons in this case could be for consistency, or just so the compiler doesn't have to look in so many places for instance variables.

    0 讨论(0)
提交回复
热议问题