In Objective-C, the @interface section lists instance variables ... isn't this a little inappropriate?

Presumably, the interface should talk about abstraction - the class interface - what methods are available, what arguments do they take, and what are the return values ​​- so are the instance variables defined in the @interface section a bit confusing?

These instance variables can be any, and they are internal implementation details - a programmer can define class Avariables using 10 instances, and another programmer can rewrite the entire class using the same interface (API) and use only 6 instance variables, so instance variables really don't relate to the section @interface, right?

Would it be more reasonable if the instance variables are listed in a separate section, for example, in a section @states, to indicate that they are internal states of an object?

+5
source share
2 answers

Initially, Objective-C classes were slightly larger than structures within structures. That is, let's say that you have a subclass of NSObject and a subclass of this subclass. The compiler efficiently combines ivars to create a structure that can encapsulate ivars for a common instance.

those.

{{{
  // @interface NSObject
  Class isa;
  }
 // @interface Subclass : NSObject
 int ivar1;
 int ivar2;
 }
// SubSubclass : Subclass
int ivar3;
}

, , ivars , , , , API .

.. " ". ivars , , , .

" ABI", Objective-C 2.0, ( - ).

" ", ivar @interface, @implementation, , @synthesize.

, ivars , , .

+5

, .

, , , @property ( ), @synthesize myProperty = _myField;, .

+1

All Articles