Tag Archives: LLVM

Sending message to nil

Recently, I’ve came across Greg Parker’s post on Sending messages to nil.

This is not actually a new information. It is quite old. However, it was new to me. So, good thing about LLVM compiler is that now even C struct‘s are returned initialized to 0 when you send message to nil.

Here is what returned when you call method on nil instance:

// when you call these methods on nil instance:

-(NSInteger)intMethod;
// any integer return type - will return 0

-(float)floatMethod;
// any float return type - will return 0.0f

-(id)objectMethod;
// any object return type - will return nil

-(CGRect)structMethod;
// any struct return type - will return struct initialized to {0}

So, starting with Xcode 4.2 and LLVM 3.0 it is safe to assume that C struct will be zeroed out when returned from nil instance method.

Check Greg’s post for details on other return types.

Advertisements