I don’t like checking if the caller passed me a nil
NSError**
in my own methods, especially if the implementation is somewhat lengthy, so I’ve put together a very basic C macro that declares an autoreleasing
NSError*
to use as temporary storage in case of a null NSError**
parameter from the caller:
1 2 3 |
#define ERROR(x,y) __autoreleasing NSError * y;\ x = x == NULL ? &y : x; \ *x = nil; |
Imagine that I have a method named foo
that takes a single NSString*
and an optional NSError**
:
1 2 3 4 |
-(BOOL)foo:(NSString*)someString error:(NSError**)error { ERROR(error,tempError); // do the work } |
Now I don’t need to check if I’ve been passed a valid NSError**
pointer, because it will be assigned to the local tempError
variable if it was nil.