Objective-C: Alternative Use of Properties?

Fri, Jul 25

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
  NSString *str;
  NSDate *date;
@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
  return @"Foo";

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.


Hey John, I remember from reading about properties the other day that the default value for a property directives is @dynamic. So, if you declare a property in your interface, but do not specify @synthesize in your implementation it will default to dynamic and expect you to implement a getter/setter corresponding to the @property you’ve defined in the implementation or at runtime.

by Paul C on Jul 25, 2008. #

Thanks Paul. What’s kind of intriguing about this approach, and the example that I was looking at, was that the method used in this manner had nothing to do with the instance variables in the class definition. The method returned a reference to a UIImage based on a specific string format (which defined the size of the image to retrieve).

This is cool for a few reasons. First, with the ‘readonly’ value specified in the property definition, there is only a getter required/implied. Second, the method can be accessed using dot syntax.

by john on Jul 25, 2008. #

I think it is because properties do not need to be directly tied to individual ivars.

How about this as an example

@interface SomeClass : NSObject
NSString *firstName
NSString *lastName
@property (copy, readwrite) NSString *firstName;
@property (copy, readwrite) NSString *lastName;
@property (readonly) NSString *fullName;


Now, in the implementation, you explicitly define the getter which operates on the other properties.

– (NSString *)fullName
NSString *retString;
retString = [NSString stringWithFormat:(@”%@ %@”, firstName, lastName)];

You could have used self.firstName and self.lastName above.

by stompy on Aug 14, 2008. #

Comments are closed on this post. You can comment on this same post which has been moved to the iPhone Developer Tips blog: http://iphonedevelopertips.com/objective-c/alternative-use-of-properties.html

by john on Aug 16, 2008. #