Archive for Programming

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)

JSON Framework v1.2

A couple days ago I released a new version of my JSON framework for Objective-C. This release saw the parser being completely rewritten to be a lot cleaner and about 10–20% faster at decoding short inputs. (Long inputs stay about the same.) This also fixed a bizarre bug, reported by David Zhao, where strings would not always be decoded properly.

I also applied a couple of patches from Greg Bolsinga. One to make the framework no longer depend on AppKit, thus really making it a Foundation framework rather than Cocoa framework, and one that adds another target to create a static library for use on the iPhone. (You will have to build this from source yourself, at the time being.)

Leave a Comment

Introducing Statistics for Objective-C

I have just released version 1.0 of Statistics: an Objective-C Foundation framework inspired by Perl’s Statistics::Descriptive. It has a similar interface, though is not a straight port.

Straight out of the box this framework provides count; min; max; range; mindex; maxdex; biased and unbiased variance and standard deviation; mode; median; percentile; arithmetic, harmonic, and geometric mean; and frequency distribution, optionally cumulative.

In addition you can return a new statistics object seeded with the same data but with outliers discarded. Perfect if you’re interested in the trimmed mean.

The code is released under the BSD license. If this sounds interesting you really should head over to the Statistics site and check out the online API docs. Said API docs, by the way, even integrates into Xcode 3 (curtsy of Doxygen). What more could you want?

Comments (2)

Follow

Get every new post delivered to your Inbox.