Add an objective @property attribute in objective-c

Posted by morticae on Stack Overflow See other posts from Stack Overflow or by morticae
Published on 2010-05-11T21:52:58Z Indexed on 2010/05/11 21:54 UTC
Read the original article Hit count: 879

Filed under:

Does anyone know of a way to add additional attribute types to the @property keyword without modifying the compiler? Or can anyone think of another way to genericize getter/setter creation?

Basically, I have a lot of cases in a recent project where it's handy for objects to lazily instantiate their array properties. This is because we have "event" objects that can have a wide variety of collections as properties. Subclassing for particular events is undesirable because many properties are shared, and it would become a usability nightmare.

For example, if I had an object with an array of songs, I'd write a getter like the following:

- (NSMutableArray *)songs {
    if (!songs) {
        songs = [[NSMutableArray alloc] init];

    return songs;

Rather than writing dozens of these getters, it would be really nice to get the behavior via...

@property (nonatomic, retain, lazyGetter) NSMutableArray *songs;

Maybe some fancy tricks via #defines or something? Other ideas?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa