UPDATE
The answer below was written before the language function of declaring instance variables in the implementation was implemented. Submitting a question is now no longer valid. As FireLizzard says @interface is no need to @interface anything to @interface .
This is a hangover because Objective-C came about as a rather thin layer built on top of C. The C path is to define the interface to the module (not to be confused with the Java interface ) in the header of the file and literally include it in each compilation unit. This is similar to automatically copying pasting declarations at the beginning of each compiled file. If this seems primitive, it is because it is there, but C is a 40-year-old language.
You must define instance variables - even private ones - in the interface, because Objective-C objects are implemented as C-structures, which themselves are simply memory blocks and named offsets inside this block. The structure representing the object of each class must contain space for instance variables of the superclass, so subclasses must know at least the size of the C structure representing the superclass, as well as the offset of the public and protected instance variables. This, unfortunately, means that all instance variables, even private ones, must be represented as part of the external interface. * C ++ another version of OO C suffers from the same problem for the same reasons.
It's a bit of a pain when you have to write down all the method signatures twice, but you get used to it.
* With the 64-bit version of the time, you no longer need to declare ivars for synthesized accessors in @interface , but since all methods are publicly available, it still means exposing the internal state to the outside world, especially since it facilitates the <href = "http: problem: //en.wikipedia.org/wiki/Fragile_base_class "rel =" nofollow noreferrer "> of a fragile base class.
source share