Nov 13
One issue with JSON that has caused problems both here at work and with Celery is the fact that a list of elements is defined by the number of elements. That is, if there are no elements it will be a NIL entry in the Dictionary, one element will return a Dictionary and multiple entries will return an Array. So, programmatically, you don’t know what you are going to get at compile time.
Fortunately, the WebService you are using is will documented… right? Secondly, you are only ever going to get 3 possible results: NIL, NSDictionary or NSArray. As such, you know when something is a list, and can handle these special cases. This is how I am managing it in Celery. If there are better ways, please, let me know.
If the WebService Specification says something might be an Array, I will also treat it as an array. If it’s a single element, I create an array and put that element into it. That way, my business logic only ever has to deal with one type (array).
id object = [dictionary objectForKey:@"key"];
NSArray* myArray = nil;
if([object isKindOfClass:[NSDictionary class]]) {
myArray = [[[NSArray alloc] initWithObjects: object,nil] autorelease];
} else {
myArray = (NSArray*)object;
}
Tagged with: JSON • objective c
Nov 08
Once I started obtaining data from FatSecret via MPOAuthConnect (see previous posts), I had a large JSON string to deal with. I did a quick Google search for a JSON library for Cocoa. I came across json-framework, which is a very simple library. It attaches a Category to NSString to provide a method called JSONValue. Calling JSONValue returns either NSDictionary or NSArray. Yet, another beauty of the langugage, Categories allow you to extend the functionality of an existing Object at runtime.
Once, I called JSONValue on the JSON string, I was able to obtain all the values needed to display the nutritional information for a particular food, and display a list of foods on a search query. This was particularly easy, and a great library.
Tagged with: categories • Celery • JSON
Nov 07
The first task I gave myself with Celery is complete. I can now search for a food, obtain a list of foods that match my search criteria, select one and get more information. This little task allowed me to learn a significant amount including:
- Using oauth and a Cocoa library to communicate
- How to build and add third party frameworks to Xcode
- How to parse JSON using a Cocoa library
- Creation and usage of Protocols
- Usage of the NSTableView class.
The first two points have been mentioned previously. I will go into more detail about the last three in future posts.
Here is a screenshot of the application.

As you can see it is quite ugly and utilitarian. This of course is not how I plan to make the app, but just needed something to learn from. One of the beauties of using MVC, which Cocoa is really built around, I can yank the View (V) and replace it with almost no code change. Just hook up the outlets to the new UI in Interface Builder and it will continue to work as before… beautiful.
Tagged with: Celery • hotdog • JSON • MPOAuthConnection • oauth • protocol • UI • Xcode