Phil
Phil

Reputation: 3035

Null or Nil to return from method?

I'm writing a method and I want to return an object or lack of object if the method cannot find what it needs to. What's the best practice to return a lack of object, should it be [NSNull null] or nil? or it doesn't make any difference?

Upvotes: 1

Views: 377

Answers (1)

Alexei Sholik
Alexei Sholik

Reputation: 7471

The convention is to return nil for Objective-C pointers (e.g. where the type is id, NSSomething *, UISomething *, etc.), NULL for arbitrary C pointers (e.g. int*, struct MyStruct*, void (*)(int).

[NSNull null] is only useful when you want to put a null value into an NS collection like NSArray or NSDictionary. So, [NSNull null] is a singleton Objective-C object that represents the null value (or the absence of value) by convention, there's nothing special about it otherwise.

Upvotes: 9

Related Questions