Adding Properties to a Category Using Associated Objects

Categories are a cool feature of Objective-C that allow you to extend existing classes with extra methods. This helps you to write really clean code by adding utility methods directly to the classes they relate to, so they can be called in a natural, object-oriented way.

A typical example of a category method is given below. Here we add an isNumeric method to NSString:

Continue reading

Alternative Use of Properties?

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:

1
2
3
4
5
6
7
8
9
@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.
Continue reading