I’m new to C, new to objective C. For an iPhone subclass, Im declaring variables I want to be visible to all methods in a class into the @interface class definition eg
@interface myclass : UIImageView {
int aVar;
}
and then I declare it again as
@property int aVar;
And then later I
@synthesize aVar;
Can you help me understand the purpose of three steps? Am I doing something unnecessary?
Thanks.
Here, you’re declaring an instance variable named
aVar:You can now use this variable within your class:
However, instance variables are private in Objective-C. What if you need other classes to be able to access and/or change
aVar? Since methods are public in Objective-C, the answer is to write an accessor (getter) method that returnsaVarand a mutator (setter) method that setsaVar:Now other classes can get and set
aVarvia:Writing these accessor and mutator methods can get quite tedious, so in Objective-C 2.0, Apple simplified it for us. We can now write:
…and the accessor/mutator methods will be automatically generated for us.
To sum up:
int aVar;declares an instance variableaVar@property (nonatomic, assign) int aVar;declares the accessor and mutator methods foraVar@synthesize aVar;implements the accessor and mutator methods foraVar