Archive for Feature Request

Objective-C syntax sugar wish list

As I mentioned in my previous post Objective-C is my (current) favourite programming language. But there things that could be improved. Creating immutable collections is a bit verbose, for example. (Although—I suspect—no more so than in for example Java.) It goes like this:


NSArray *array = [NSArray arrayWithObjects:
	@"one", @"two", @"three", nil];

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
	[NSNumber numberWithInteger:1], @"one",
	[NSNumber numberWithInteger:2], @"two",
	[NSNumber numberWithBool:YES], @"true",
	nil];

However, you don’t have to use this syntax for strings. Static strings are created with a special syntax, like so:

NSString *string = @"This is a string";

It would be great if this could be extended to arrays and dictionaries. The syntax would be something like this:


NSArray *array = @[ @"one", @"two", @"three" ];

NSDictionary *dict = @{
	@"one", [NSNumber numberWithInteger:1],
	@"two", [NSNumber numberWithInteger:2],
	@"true", [NSNumber numberWithBool:YES],
 };

(Note that for the dictionary I took the liberty of fixing the argument order so that it makes sense to me.)

Creation of NSNumbers is another area that could benefit from the same trick. Although NSNumber instances can be initialised in lots of different ways, I think this new syntax sugar should concern itself with just three: NSInteger, double and BOOL. We would then get this syntax:


@234    // equivalent to [NSNumber numberWithInteger:234]
@-14    // equivalent to [NSNumber numberWithInteger:-14]
@12.0   // equivalent to [NSNumber numberWithDouble:12.0]
@-0.01  // equivalent to [NSNumber numberWithDouble:-0.01]
@YES    // equivalent to [NSNumber numberWithBool:YES]
@NO     // equivalent to [NSNumber numberWithBool:NO]

The initial dictionary creation example would then simply become:


NSDictionary *dict = @{
	@"one", @1,
	@"two", @2,
	@"true", @YES,
};

I've filed a feature request with Apple, rdar://problem/6171253, I don't expect, much, that this wish is heeded. A man can dream, though; a man can dream...

Comments (3)

Objective-C feature request

dictionary.pngObjective-C, particularly after it became 2.0, is my favourite language. It’s partly because of the language itself, but I guess mostly because of the libraries. The Cocoa libraries are awesome.

For the most part. No software is perfect, and even in Cocoa there are things that could be improved. One thing that continue to trip me up is one of NSDictionary‘s initialiser methods.

Dear Apple,

The dictionaryWithObjectsAndKeys: always end up confusing me, as the arguments always seem in the wrong order. It would make much more sense to have the method dictionaryWithKeysAndObjects: where you specified a list of key/value pairs, rather than value/key pairs.

Yours Sincerely,

Stig

Here’s a random sample of the above from the Adium source code. Honestly. I grabbed the first sample from it I could find. I did not hunt for a particularly egregious example:


NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSValue valueWithPointer:SecKeychainLockAll], AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTION,
    @"SecKeychainLockAll", AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME,
    AI_LOCALIZED_SECURITY_ERROR_DESCRIPTION(err), AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION,
    nil];

Doesn’t that make a lot more sense like this?


NSDictionary *userInfo = [NSDictionary dictionaryWithKeysAndObjects:
    AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTION, [NSValue valueWithPointer:SecKeychainLockAll],
    AIKEYCHAIN_ERROR_USERINFO_SECURITYFUNCTIONNAME, @"SecKeychainLockAll",
    AIKEYCHAIN_ERROR_USERINFO_ERRORDESCRIPTION, AI_LOCALIZED_SECURITY_ERROR_DESCRIPTION(err),
    nil];

This has confused me so many times now that I have filed a feature request with Apple (rdar://problem/6171269) to have a new method with the arguments in the “right” order. (Wondering if that url is broken? See here.) Here’s to hoping that they honour it and put it in Snow Leopard…

Comments (1)

Follow

Get every new post delivered to your Inbox.