Possible Duplicate:
why is “error:&error” used here (objective-c)可能重复:为什么这里使用“error:&error”(objective-c)
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
What does the &
symbol mean in the above code?
上面代码中的&符号是什么意思?
3 个解决方案
#1
1
It's the address-of operator; it produces a pointer pointing to the referent. In this case, error
is an NSError *
; AVCaptureDeviceInput deviceInputWithDevice:error:
takes an address to it and may modify error
through that pointer to indicate the error that occurred.
这是取地址运算符;它生成指向引用的指针。在这种情况下,错误是一个NSError *;error:获取一个地址,并可以通过该指针修改错误,以指示发生的错误。
#2
4
error is a pointer to an error object. &error is a pointer to a pointer to an error object. If you pass a method a pointer to your pointer to an error and then an error occurs, the method can allocate an error object and set your error pointer to point to it.
错误是指向错误对象的指针。&error是指向错误对象的指针。如果你给一个方法一个指向错误的指针的指针,然后一个错误发生了,那么这个方法就可以分配一个错误对象,并设置你的错误指针指向它。
#3
3
The & symbol is used to pass the address of the variable that is prefixed. It is also called pass by reference as opposed to pass by value. So &error means the address of error. Since error is defined as a pointer, then &error is the address of the pointer to the actual store of error.
&符号用于传递前缀的变量的地址。它也被称为通过引用传递,而不是通过值传递。所以&error是错误的地址。由于错误定义为指针,那么&error就是指向实际错误存储的指针的地址。
The method you call may have the parameter defined as **error, thus forcing you to pass a pointer to the pointer, or reference to the pointer.
您调用的方法可能具有定义为**error的参数,从而迫使您将指针传递到指针,或引用到指针。
If a method uses *error as the parameter, the method can change the value pointed to by the parameter. This value can be referenced by the caller when the method returns control. However, when the method uses **error as the parameter, the method can also change the pointer itself, making it point to another location. This means that the caller's error variable will hence contain a new pointer.
如果方法使用*error作为参数,则该方法可以更改参数所指向的值。当方法返回控件时,调用者可以引用此值。但是,当方法使用**error作为参数时,该方法还可以更改指针本身,使其指向另一个位置。这意味着调用者的错误变量将包含一个新的指针。
Here is an example
这是一个例子
-(void)changePointerOfString:(NSString **)strVal {
*strVal = @"New Value";
}
-(void)caller {
NSString *myStr = @"Old Value"; // myStr has value 'Old Value'
[self changePointerOfString:&myStr]; // myStr has value 'New Value'
}
#1
1
It's the address-of operator; it produces a pointer pointing to the referent. In this case, error
is an NSError *
; AVCaptureDeviceInput deviceInputWithDevice:error:
takes an address to it and may modify error
through that pointer to indicate the error that occurred.
这是取地址运算符;它生成指向引用的指针。在这种情况下,错误是一个NSError *;error:获取一个地址,并可以通过该指针修改错误,以指示发生的错误。
#2
4
error is a pointer to an error object. &error is a pointer to a pointer to an error object. If you pass a method a pointer to your pointer to an error and then an error occurs, the method can allocate an error object and set your error pointer to point to it.
错误是指向错误对象的指针。&error是指向错误对象的指针。如果你给一个方法一个指向错误的指针的指针,然后一个错误发生了,那么这个方法就可以分配一个错误对象,并设置你的错误指针指向它。
#3
3
The & symbol is used to pass the address of the variable that is prefixed. It is also called pass by reference as opposed to pass by value. So &error means the address of error. Since error is defined as a pointer, then &error is the address of the pointer to the actual store of error.
&符号用于传递前缀的变量的地址。它也被称为通过引用传递,而不是通过值传递。所以&error是错误的地址。由于错误定义为指针,那么&error就是指向实际错误存储的指针的地址。
The method you call may have the parameter defined as **error, thus forcing you to pass a pointer to the pointer, or reference to the pointer.
您调用的方法可能具有定义为**error的参数,从而迫使您将指针传递到指针,或引用到指针。
If a method uses *error as the parameter, the method can change the value pointed to by the parameter. This value can be referenced by the caller when the method returns control. However, when the method uses **error as the parameter, the method can also change the pointer itself, making it point to another location. This means that the caller's error variable will hence contain a new pointer.
如果方法使用*error作为参数,则该方法可以更改参数所指向的值。当方法返回控件时,调用者可以引用此值。但是,当方法使用**error作为参数时,该方法还可以更改指针本身,使其指向另一个位置。这意味着调用者的错误变量将包含一个新的指针。
Here is an example
这是一个例子
-(void)changePointerOfString:(NSString **)strVal {
*strVal = @"New Value";
}
-(void)caller {
NSString *myStr = @"Old Value"; // myStr has value 'Old Value'
[self changePointerOfString:&myStr]; // myStr has value 'New Value'
}