开发者

& with variable [duplicate]

This question already has answers here: Closed 11 years ago.

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 e开发者_运维百科rror:&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:

#include <stdio.h>

// define a function that takes a pointer to an integer as argument
void change_value_of_int(int* int_to_change) {
    // change the value to which the argument points
    *int_to_change = 5;
}

int main() {
    // create a stack variable
    int test_int = 10;

    // pass the address of test_int to the function defined earlier
    change_value_of_int(&test_int);

    // now the value of test_int is 5
    printf("%d\n", test_int);

    return 0;
}

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 with change_value_of_int(test_int). You must send it the address of the test_int variable, not the variable itself (because if you send a copy of the variable, it can't change it).

Same thing with the NSError* example. jsonParser expects the address of a NSError*, not a NSError*, therefore the method is defined as:

- (id)objectWithString:(NSString*)jsonrep error:(NSError**)error;

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 the error argument) becomes the return value of [errorTrace lastObject].


It's the address of operator, found in C, C++ and Objective-C.

In your example, &error yields a NSError ** (that is, a pointer to a pointer).

This is common in C (and, by extension, Objective-C): pass-by-reference is simulated with pointers, which means you have to pass the address of the object you want to modify (in this case, another pointer) to a function.


& is the address operator. real quick lesson, all objects in Objective-C are pointers, which makes thing easy, because you know the level of indifference of everything, id is a special case, which a pointer to any object, without specifying class.

pointer to a pointer is most often used when returning an error, that is passed as a parameter to a method.

-(void)doSomething:(NSError**)err
{
    //we have an error, return it.
    *err = [NSError errorWithDomain:@"custom Domain" code:42 userInfo:nil];

}

then use the method with:

    NSError * err = nil;
    [self doSomething:&err];
    if(err)
    {
        NSLog(@"we have an error in domain: %@",[err domain]);
    }

which outputs:

we have an error in domain: custom Domain

this is especially useful when you are returning something, as you can only return one value in C. This is exceptionally common is vanilla C and C++, but not used that much in Objective-C.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜