hit counter

Timeline

My development logbook

Need to Write a Proper Unit Test in Objective-c

It is my first attempt at testing my JSON handling code:

1
2
3
4
5
6
7
- (void)testReadingJSON
{
    QueQuestionManager* qm = [[QueQuestionManager alloc] init];
    qm.server_url = @"http://localhost:8777/";
    [qm readQuestionnaire];
    NSLog(@">> %@", qm.questionnaire);
}

Needless to say it is pretty bad, for the following reasons:

  • It has an external dependency of a local server. It is strictly speaking not a unit test

  • The code does not work. It is likely because the run loop is not started when the unit test runs

My TODO:

  • Refactor the above code to be a proper unit test

  • Use functional testing framework to implement the above test

Using NSURLConnection to Read Json Data

My code is not good at all for the following reasons:

  • The NSURLConnection retrieves data synchronously

  • The decoding of JSON is not neat. A strong code smell

Synchronous network call

Next step: rewrite the below code to asynchronous mode.

In the viewDidLoad method of my ViewController, I called the following method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-(BOOL) readJSON {

    NSError *error;
    NSURLResponse *response;

    receivedData = [NSMutableData data];

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:_server_url]];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        NSData *result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
        NSDictionary* json = [result objectFromJSONDataWithParseOptions:(JKParseOptionNone)];
        [self convertJsonIntoMyData:json];
        return TRUE;
    } else {
        return FALSE;
    }
}

Decoding of JSON data

I used a lot of enumerators to walk through dictionaries and arrays at different level. It makes future changes to the JSON schema or objective-c object model difficult.

Cannot Init a Class Object

Got this exception

1
2
2013-12-16 17:35:59.976 MyProject[41964:70b] *** Terminating app due to uncaught 
exception 'NSInvalidArgumentException', reason: '*** +[MyClass<0x100084d80> init]: cannot init a class object.'

It is because I have mistakenly written

1
[[MyClass init] alloc];

instead of

1
[[MyClass alloc] init];

Incorrect Way to Use JSONKit?

At the beginning I use the following code to decode a JSON string:

1
2
3
4
5
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    JSONDecoder* decoder = [[JSONDecoder alloc]
                            initWithParseOptions:JKParseOptionNone];
    NSArray* json = [decoder objectWithData:receivedData];

and then I tried to convert the json data with this function:

1
2
3
4
5
6
-(voide) convertJSONData:(id)json {
    NSEnumerator *enumerator = [json objectEnumerator];
    id anObject;

    while (anObject = [enumerator nextObject]) {
        // test for anObject class and handle accordingly

It is probably not an efficient way to handle the incoming json data.