Archive for Objective-C

Book review: Beginning iPhone Development

I have been going through this book for the past few weeks, and so far it’s the best book around for developers beginning with Objective-C and the iPhone SDK. The writing is natural (and that’s hard to find these days), detailed and sometimes funny in a quirky way. I appreciate the fact that they use lots of screenshots to explain the interactions with Interface Builder and Xcode.

They have Chapter 3 (Handling Basic Interaction) available for free online if you would like to read a sample of the book. Highly recommended.

You may also like to follow Jeff LaMarche’s blog, one of the co-authors of this book.

iPhone SDK: Parsing JSON data

So when developing for the iPhone with the official SDK, and trying to parse JSON data, there are a few options for JSON libraries to use:

  • json-framework – General purpose JSON library for Objective-C, but contains a custom SDK that was built to be easily bundled with iPhone app projects.
  • TouchJSON – Part of the TouchCode, a project of a bunch of classes that are targetted for iPhone development, so it’s deemed to be a very light and fast library.
  • Maybe more? Let me know in the comments area.

Both are very under-documented (I mean, SERIOUSLY), but if you poke at them long enough, things will start making sense. I personally chose to use json-framework, as I initially had a few problems figuring out how to parse JSON content that had an array as the root object. Apparently TouchJSON officially only supports JSON results serialized as a object in the root level. As in:

{"values": ["value1", "value2", "value3"]}

Instead of:

["value1", "value2", "value3"]

That’s usually not a big deal, as you can just change the way you generate the JSON result in your server side code (if you are dealing with web services returning the JSON data), but what happens if you already have code expecting the server side to return that type of result?

Either way, I’m going to provide a simple usage example of json-framework to parse a JSON string. In order to install json-framework, you should follow the simple instructions available in its INSTALL file.

NSString *jsonString = @"[\"value1\", \"value2\", \"value3\"]";
SBJSON *json = [[SBJSON alloc] init];
NSError *error = nil;
NSArray *results = [json objectWithString:jsonString error:&error];
[json release];

For my particular application, I was fetching the JSON string from a web service, and it was coming up as a NSData object, so I had to do the following:

NSString *jsonString = [[NSString alloc] initWithData:resourceData encoding:NSUTF8StringEncoding];
SBJSON *json = [[SBJSON alloc] init];
NSError *error = nil;
NSArray *results = [json objectWithString:jsonString error:&error];
[json release];
[jsonString release];

Hopefully this is useful to someone else.

Xcode keyboard tricks

I’m mostly a keyboard navigation type of user, so it’s been a few busy months trying to remember all of the different little tricks in Xcode while doing Objective-C work.

A few of my favorites:

  • ⌘ + l (lowercase L) – if done within a text editor pane, it will re-center the editor around the line that the cursor is currently in. Too confusing? Try this video demonstration from Todd Ditchendorf then.
  • ⌘ + option + up arrow – switches between header (.h) and implementation (.m) files.
  • ⌘ + b – builds project.
  • ⌘ + r – build and run project.
  • If you hold down the alt/option key, and then double click on a code token, such as a class name or function name, it will automatically search the documentation for that particular word. Very useful when you want to read some more about the UITableView class, for instance.
  • A similar trick is available if you would like to open the definition for a particular code token, such as the source code file where class ExampleController is defined. Just hold down the command key (⌘), and double click the word.

Creating UIColor objects from hex values

It’s pretty inconvenient to create UIColor objects while developing apps for the iPhone, as you need to specific separate values for the RGB parts: red, green, blue.

I found a trick online a few weeks ago on how to automatically generate that code, by simply using a macro:

#define UIColorFromRGB(rgbValue) [UIColor \
    colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
    green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
    blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

The usage looks something like this:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"] autorelease];
    cell.textColor = UIColorFromRGB(0x333333);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.text = @"Testing 1 2 3";

Hope that’s useful to someone.

« Previous Page « Previous Page Next entries »