Cocoa, NSStrings and regular expressions

Two days into my programming-related summer project and I’m really enjoying it. Admittedly, I spent my lunch hour today in battle against Perl’s DBD::mysql module which appears to be somewhat difficult to install on Mac OS X. In the end, I just gave up and did all my Perl scripting by SSH on the box hosting the database.

Anyway, that’s all beside the point… Cocoa programming is looking like it’s shaping up to be extremely powerful and quite fun now I’ve started to get into the object-oriented mindset. The program that I’m working on deals with a lot of text file formats including .mat, FASTA, GFF and .xms (quasi-XML). So, as you can probably imagine, NSString’s support of the following methods (amongst its large arsenal) came as a great relief:
+ (id)stringWithContentsOfFile:(NSString *)path
- (NSArray *)componentsSeparatedByString:(NSString *)separator

However, the relief ebbed slightly when it dawned upon me that neither Objective-C nor the Cocoa frameworks appeared to have any support for regular expressions. The very staple of text parsing appeared to be absent! So, before you could Google “Cocoa regular expression”, I was contemplating ways to outsource regular expression matching to shell scripts — maybe I could just do it in Perl or sed? But then I came to my senses and actually Googled “Cocoa regular expression”.

This resulted in (at least) 2 interesting leads, both of which are actually “native” solutions (i.e. don’t require the use of any non-standard frameworks). The first lead came from Cocoadev’s NSString page, which lists two undocumented methods of NSString:
- (BOOL)matchesPattern:(id)aPattern;
- (BOOL)matchesPattern:(id)aPattern caseInsensitive:(BOOL)flag;

I haven’t yet had a chance to try these out, but if they work as one would expect, they’d at least show that Apple might be interested in adding regular expression support to NSString. Maybe these will be exposed and documented in Leopard?

The second “native” solution I stumbled upon makes great use of a class that was introduced in Mac OS 10.4 to support Core Data: NSPredicate. There’s a nice little example at Simon Stiefel’s site, where I originally found the NSPredicate idea.

Aside from these two undocumented and/or unsupported methods, there are a multiplicity of thrid party methods to run NSStrings against regular expressions, a large number of which are documented here. The presence of rudimentary regular expression matching in NSString makes me hopeful that Apple will expand on and document regexp support, which I reckon is currently a gaping hole in Cocoa’s otherwise impressive arsenal…

iPhone could use Web Clip

Since I’m in the UK, I don’t own an iPhone yet, but I, like many others, was still disappointed to discover that Apple was not releasing a “proper” SDK for the iPhone. The move has a number of unpleasant consequences:

  • developers must code their interface rather than lay it out using a GUI tool like IB
  • the app must be hosted on a server in order for it to run – there’ll be no permanent downloading here
  • as a result of being hosted, apps will (often) have to load over EDGE
  • all apps will be surrounded by the browser chrome
  • no local facilities such as Bluetooth or the camera are available to apps. Even the games claiming to make use of the accelerometer actually just use the width of Safari’s viewport to judge the phone’s orientation

All of these and more have been discussed at length all over the web already, so I’ll leave that bit to the experts. My idea in no way alleviates the need for a proper SDK, but I think it would be really neat for the iPhone to support the Web Clip feature from Leopard (see here and here). The premise is simple – it would allow the user to load a web page and then select a portion of it using the iPhone’s nifty pinching and scrolling much like when setting a wallpaper. Once the user was happy with the selection, the web clip could then be saved into the 17th iPhone app:
iPhone web clips app.
OK, so I didn’t get the corner radii of that icon right, but I do think that it could be a pretty neat feature. Then maybe when Apple’s sorted out the actual API (which is surely on the way, right, Apple?), widget support could be brought up to the level that it’s at in desktop Mac OS X with Cocoa-based plug-ins and support for scripting, augmenting the web clip feature to a fully fledged dashboard (albeit one widget at a time).

I admit that this idea doesn’t solve all of the problems with developing for the iPhone browser; web clips still have to download the whole page, so it certainly wouldn’t reduce bandwidth consumption. I do, however, think that it might just speed things up a little, negating the need to zoom and scroll around a web page when there’s only one bit you’re interested in…