Preventing crashes: JSON Parsers & Null entries!

How many times you got to parse a backend response, and tested if a given value is != nil?. Well, as it turns out, many parsers (such as JSONKit) will parse ‘null’ values into an instance of NSNull. That might cause a crash… unless you write eeeverywhere… ‘!= nil && != [NSNull null]’.

So… a good idea would be to implement an NSDictionary extention, to do that. Right?.
The method would look like this:
[cc lang=”objc”]
– (id)objectForKeyNotNull:(id)key 
{
    id object = [self objectForKey:key];
    if (object == [NSNull null])
    {
        return nil;
    }
    else
    {
        return object;
    }
}[/cc]

Filtering arrays with NSPredicate!

Suppose you wanna filter a collection of objects. Normally, you’d write a while loop, implement a comparison, and add the matching objects to a collection.

Well, there is an easier way!!. For the sake of this example, say you wanna filter the objects that have the BOOL ‘isEnabled set to YES. So, you could do the following:

[cc lang=”objc”]
NSPredicate* predicate = [NSPredicate predicateWithFormat:@”isEnabled == YES”]; NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate];
[/cc]

Yet another powerful example of NSPredicate would be… remember ‘SELECT* WHERE FIELD IN(1, 2)’ ?. Guess what!

[cc lang=”objc”]
NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:@”field IN %@”, desiredFieldValues];
NSArray* filteredObjects = [allObjects filteredArrayUsingPredicate:filterPredicate];
[/cc]

Memory Management Tips!

It’s REALLY recommended that you set to nil any pointer that has a reference to a released object. But it’s tedious, right?. [pointer release]; pointer = nil;.

Why don’t we use a macro instead??
[cc lang=”objc”]
#define LAWipe(x)         [x release]; x = nil;
[/cc]

Simple. Nice. This should help you lower down the BAD_ACCESS crashes!.

 

iOS Singletons

ios3

Suppose that… for whatever reason, you need to target iOS 3.x. Yes. You need to build an app that should run on any possible device. Or say that… you simply don’t wanna use GCD.

What’s the alternative to write a singleton?
[cc lang=”objc”]
+ (id)sharedInstance
{
if(_instance = nil)
{
@synchronized(self)
{
if(_instance == nil)
{
_instance = [[[self class] alloc] init];
}
}
}
return _instance;
}[/cc]
This is a nice alternative to the GCD option. The first time the instance is created, you’ll suffer the lag caused by the @synchronized block. But after that. it’s just an if. No context switch. No whatsoever! REALLY performant!

iOS Assertions

So… you write a new piece of code. You run it. Test it. Ship it!. And in two weeks, you end up flooded by iTunes Connect Crash Reports.

What happened?. It turned out that an attribute parsed from a backend response ended up being a NSString instead of an NSNumber. And it was only after an untested-and-specific-workflow that your application actually used that field.

How do we prevent this?. ASSERTIONS!. A good friend of mine teached me that… great software blows everywhere before getting published. If your code works perfectly… you should be scared!.

What are assertions?. Simple. Checkpoints that will produce controlled crashes while your app is in Debug mode. How do you use them?.

Well, you’ll need to declare few macros first. Say… we name this include ‘LADebug.h’:
[cc lang=”objc”]
#ifdef DEBUG
#   define ASSERT(x)            assert(x)
#   define ASSERT_CLASS(x, y)   assert([x isKindOfClass:[y class]])
#else
#   define ASSERT(x)           
#   define ASSERT_CLASS(x, y) 
#endif
[/cc]

So.. those macros will be available only if the DEBUG flag is present. How do you use them?…. this is just a simple example…

[cc lang=”objc”]
– (void)processResponse:(NSArray*)array callback:(id)callback

ASSERT_CLASS(array, NSArray);
ASSERT(callback);

}
[/cc]

This example will procude a controlled crash if the parameter ‘array’ is not an instance of NSArray. Or if the ‘callback’ parameter is not present.

This will make your debug target a little heavier. But if you use this app-wide, you’ll be able to detect problems early in the development stages, and you’ll have the chance of fixing them!.

Cool, ha?. (Thanks Tincho!).

 

%d