Sunday, 25 August 2013

Property getter implementation

Property getter implementation

I always see something like this:
- (NSMutableArray*)items {
if (_items == nil)
_items = [NSMutableArray array];
return _items;
}
Recently I wrote:
- (NSMutableArray*)items {
return _items ? _items : (_items = [NSMutableArray array]);
}
And recently I knew about the ?: operator:
- (NSMutableArray*)items {
return _items ?: (_items = [NSMutableArray array]);
}
I know all three ways work similarly, but I'm concerned about readability.
What do you think?

No comments:

Post a Comment