I started writing a list of ways I thought Objective-C could be improved, and I realized that many of my wishes involved more compact syntax. For example [array objectAtIndex:1]
is so verbose I think it diminishes readability, compared to array[1]
.
I can’t quite match that brevity (can you, by using Objective-C++?), but with a one-line category, you can say, x = [array:1];
.
@interface NSArray (ConciseLookup) - (id):(NSUInteger)index; @end @implementation NSArray (ConciseLookup) - (id):(NSUInteger)index; { return [self objectAtIndex:index]; } @end
My question is: do you find this compact “syntax” useful at all, or is it added complexity with no substantial code compression? Personally I think the latter, but the number of wishes I had involving more concise Objective-C syntax makes me wonder…
I think a good compromise is defining a method named “at:” for concise lookup, like in Smalltalk. I’d like to see it added to Foundation.
Comment by Paul — May 15, 2009 @ 2:54 pm
I like
-at:
, it’s a good compromise! And it’s easy enough to add yourself.Every Objective-C project of mine includes a .m/.h file that defines:
IsEmpty()
, syntactic sugar for logging,-isEqualToURL:(NSURL*)otherURL;
, and (for Objective-C 1.0 projects)foreach
. It’s worked out very well, and I’ve never had to mess with frameworks or libraries.Comment by Vincent Gable — May 15, 2009 @ 3:14 pm