While scanning a few code examples inside the iPhone SDK, I bumped into something interesting. I wrote a short example to mimic what I found…take a look at the following interface declaration:
@interface SomeClass : NSObject
@property (nonatomic, retain) NSString *str;
@property (nonatomic, retain) NSDate *date;
@property (readonly) NSString *testSomething;
The only aspect that should standout is the property declaration for testSomething . Notice there is no instance instance variable tied to this declaration. This is different if for no reason other than the typical use for properties is as a shorthand for declaring accessor methods for instance variables.
So what happens if we now add an instance method, such as shown below, inside the implementation file for SomeClass (and don’t include either a @synthesize or @dynamic declaration)?
- (NSString *) testSomething
Well, for one thing, we can now access the testSomething method using dot syntax.
SomeClass *ptr = [[SomeClass alloc] initWithStrAndDate:@"Fubar"];
NSLog(@"test: %@", ptr.testSomething);
This is intriguing. I couldn’t find any specific references to using properties in this manner. Is this simply a side-effect of how properties are implemented?
If you can shed any light on this, including whether this is a common/good practice, please post a comment.