ios - Why must I define variables twice in the Header file? -
why must define variables twice in header file? differences there between these variables?
the first definition here:
@interface mycontroller: uiviewcontroller { nsinteger selectedindex; }
the second definition here:
@property (nonatomic) nsinteger selectedindex;
what you're seeing required in earlier versions of objective-c, isn't more.
in first versions of objective-c used next until new runtime introduced (with objective-c 2.0 on mac os x), instance variables had declared part of class's structure in
@interface
. reason if subclassed class, compiler needed know instance variable layout of class see @ offset put subclass's instance variables.when properties introduced, synthesized properties had "backed" instance variable in class's structure. therefore had declare both instance variable , property.
all of above no longer true. newer objective-c less fragile in way looks instance variable offsets, has meant few changes:
- not instance variables need in
@interface
. can defined in@implementation
: though not in categories due possibilities of clashing , other issues.- instance variables synthesized properties can inferred , created based on property definition.
- you can programmatically add instance variables classes you're creating @ runtime (only before you've registered class available system).
so, reiterate, needed declare both instance variable , synthesized property in older versions of objective-c language. you're seeing redundant , should not considered "best practice".
Comments
Post a Comment