Possible Duplicate:
Why passing &error instead of error in Cocoa programming?
I have a question for which I cannot seem to find an answer…
I am using the SBJsonParser and there is a line of code I find puzzling:
NSError *error;
self.jsonData = [jsonParser objectWithString:responseString error:&error];
What is the & in front of the error parameter? (&error)?
In Objective-C, just like in C,
&is the “address-of operator” and it returns the address of its argument. To find out more about it, I recommend you read this short chapter from The C Book.Here’s an example of how the operator is used, to get a better idea:
Note that the
change_value_of_int()function expects the first parameter to be a pointer to an int, not an int, so you can’t call it withchange_value_of_int(test_int). You must send it the address of thetest_intvariable, not the variable itself (because if you send a copy of the variable, it can’t change it).Same thing with the
NSError*example.jsonParserexpects the address of aNSError*, not aNSError*, therefore the method is defined as:Take a look at the header file and at the implementation to see how it is used. The value of your
error(*error= the value of the thing pointed to by theerrorargument) becomes the return value of[errorTrace lastObject].