Why are instance variables defined in the header file in Objective-C

前端 未结 6 1490
一个人的身影
一个人的身影 2021-01-12 22:36

I can understand defining the functions in the @interface of the header file, but why the instance variables? Shouldn\'t the instance variables be private, only accessible

6条回答
  •  有刺的猬
    2021-01-12 22:52

    Taken from the section of Apple's documentation on defining Objective-C classes, The Role of the Interface:

    Although instance variables are most naturally viewed as a matter of the implementation of a class rather than its interface, they must nevertheless be declared in the interface file. This is because the compiler must be aware of the structure of an object where it’s used, not just where it’s defined.

提交回复
热议问题