Whats the difference between a property and an instance variable in Objective-C. I need to understand this in OOP terms. Is a property declaration just a convenience wrapper (with @synthesize in the implementation) for accessing instance variables?
thanks,
codecowboy.
Most of the core iOS and MacOs software is still written in Objective-C, though Apple is pushing for new updates to be written in Swift.
It's because Objective C has been the de facto language for Mac OS X development before it was Mac OS X. When Jobs left Apple to set up NeXT, the language Objective C was developed as a specific language that wasn't C++ and avoided many of its pitfalls.
Objective-C was the standard programming language supported by Apple for developing macOS (which descended from NeXTSTEP) and iOS applications using their respective application programming interfaces (APIs), Cocoa and Cocoa Touch, until the introduction of Swift in 2014.
The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it. Properties are basically the accessor methods.
Properties and ivars are two completely different things.
And instance variable is a variable stored inside the object, so each instance has its own. It is referenced by pointer addition relative to the object pointer/self (slightly indirected for the modern runtime, but functionally equivalent). ivars are generally internal to a class, and by default can only be accessed by the class and its descendents (@protected). Within methods they are available with no qualification, otherwise they can (but rarely are, ad usuaually should not) be accessed via indirection, eg obj->ivar.
A property defines a getter and setter (the setter is optional) interface. That's all it does. It defines two public methods:
- (TYPE) propname;
- (void) setPropname: (TYPE) newPropname;
These are defined as methods exactly as if you declared them like that, no more, no less. These methods are called either with the normal syntax ([obj propname] and [obj setPropname:n] or using the modern dot notation (obj.propname or obj.propname = n). These two options are syntactically different only, they behave identically, and you can use dot notation whether the methods are declared with @property or declared manually as above.
You must then implement the methods in the implementation, either by writing the methods yourself, by using @synthesize, or by handling the missing method dynamically.
Properties may be backed by an ivar (named the same or named differently (my preference to avoid confusion)), or they may not. They may store their value elsewhere, or they may calculate it from other data.
For example, you might have:
@property (nonatomic, readonly) NSString* fullname;
and then implement - (NSString*) fullname to return the concatenation of firstname and lastname.
I think you are pretty much there. The @property and @synthesize make the accessor declarations and implementation for the already declared ivar. You have various attributes you can define on the @property too giving you control over how it is generated to make it appropriate for the ivar
Have a look at "Objective C 2.0 Declared Properties"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With