Is there a groovy way to do this in ObjC?


You gotta be kidding me!!! I’m writing some code TDD style for a change. I’m trying to do this in an X-Code iPhone project, knowing all too well the pain of Objective-C verbosity. I get to the point where I wanna load a test resource. Because there’s no getClass().getResourceAsStream in Obj-C iPhone land I’m reduced to this:
+(NSData*) loadTestResourceData:(NSString*)resourceName
{
NSRange range = [resourceName rangeOfString:@"." options:NSBackwardsSearch];
NSString *file = [resourceName substringToIndex:range.location];
NSString *ext = [resourceName substringFromIndex:range.location + 1];
NSString *path = [[NSBundle bundleForClass:[MyTestSupport class]] pathForResource:file ofType:ext];
return [NSData dataWithContentsOfFile:path];
}

There has got to be justice here! I miss the days when I could post a question on Nabble saying, “Is there a groovy way to do this? [code-snipet]” At any rate, I’m really upset because I can’t get CoverStory gcov stuff working with my iPhone projects. I don’t particularly care about test coverage but I did find some good usage for coverage tools. Do you do iPhone stuff? Have you used CoverStory? Can you help?

3 thoughts on “Is there a groovy way to do this in ObjC?

  1. Looks like you’re doing it right to me!🙂

    Though you could skip the whole parsing out the extension business with a [resourceName pathExtension] and a [[resourceName lastPathComponent] stringByDeleteingLastPathExtension], or theoretically you just pass the full filename for pathForResource nil for the ofType: (or so the docs say)

    Haven’t tried. YMMV

    If a reel Cocoa nurd were writing this, he’d write a special loader class that took the extension for wrote and just accepted the file name. (Just teasing).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s