I’m trying to understand Objective-C properties and I have some lingering questions about their syntax.
What is the difference between explicitly declaring an ivar for a property like this:
@interface myObject1 : NSObject {
NSString *title;
}
@property (copy) NSString *title;
@end
and this:
@interface myObject2 : NSObject {
}
@property (copy) NSString *title;
@end
The myObject2 example seems to work. Is it OK to implement properties as in myObject2 or should the associated ivar always be explicitly defined?
What are the ramifications of not explicitly declaring the ivar?
On the modern Objective C runtime (nonfragile-abi) they are the same, the ivar backing will be created automatically by the @synthesize declaration. This is the runtime used by iPhone, and 64 bit Mac OS X apps. 32 bit Mac OS X use the legacy runtime, where it is not possible to synthesize the ivar, and the second bit of code you wrote would not compile properly.
The most recent versions of the iPhone simulator use the modern runtime, but older ones don’t. So while both code examples will work on actually iPhones (synthesizing the necessary storage), the second example will fail to compile for the simulator unless you have an up to date Xcode.