All*_*lyn 51 c cocoa pointers objective-c multiple-indirection
所以,我看到了这个:
error:(NSError **)error
Run Code Online (Sandbox Code Playgroud)
在苹果医生.为什么两颗星?有什么意义?
mip*_*adi 68
"双星"是指向指针的指针.所以NSError **是一个指针的指针类型的对象NSError.它基本上允许您从函数返回错误对象.您可以NSError在函数中创建指向对象的指针(调用它*myError),然后执行以下操作:
*error = myError;
Run Code Online (Sandbox Code Playgroud)
将该错误"返回"给调用者.
在回复下面发表的评论时:
你不能简单地使用一个NSError *因为在C中,函数参数是按值传递的 - 也就是说,传递给函数时会复制值.为了说明,请考虑以下C代码片段:
void f(int x)
{
x = 4;
}
void g(void)
{
int y = 10;
f(y);
printf("%d\n", y); // Will output "10"
}
Run Code Online (Sandbox Code Playgroud)
xin 的重新分配f()不会影响f()(g()例如)之外的参数值.
同样,当指针传递给函数时,会复制其值,并且重新赋值不会影响函数外部的值.
void f(int *x)
{
x = 10;
}
void g(void)
{
int y = 10;
int *z = &y;
printf("%p\n", z); // Will print the value of z, which is the address of y
f(z);
printf("%p\n", z); // The value of z has not changed!
}
Run Code Online (Sandbox Code Playgroud)
当然,我们知道我们可以z很容易地改变指出的价值:
void f(int *x)
{
*x = 20;
}
void g(void)
{
int y = 10;
int *z = &y;
printf("%d\n", y); // Will print "10"
f(z);
printf("%d\n", y); // Will print "20"
}
Run Code Online (Sandbox Code Playgroud)
所以有理由认为,要改变点的值NSError *,我们还必须传递一个指向指针的指针.
小智 6
双星(**)表示法不是特定于初始化类中的变量.它只是对对象的双重间接引用.
float myFloat; // an object
float *myFloatPtr; // a pointer to an object
float **myFloatPtrPtr; // a pointer to a pointer to an object
myFloat = 123.456; // initialize an object
myFloatPtr = &myFloat; // initialize a pointer to an object
myFloatPtrPtr = myFloatPtr; // initialize a pointer to a pointer to an object
myFloat; // refer to an object
*myFloatPtr; // refer to an object through a pointer
**myFloatPtrPtr; // refer to an object through a pointer to a pointer
*myFloatPtrPtr; // refer to the value of the pointer to the object
Run Code Online (Sandbox Code Playgroud)
双指针表示法用于调用者打算通过函数调用修改其自己的指针之一,因此指针的地址而不是对象的地址被传递给函数.
一个例子可能是使用链表.调用者维护指向第一个节点的指针.调用者调用函数来搜索,添加和删除.如果这些操作涉及添加或删除第一个节点,则调用者的指针必须更改,而不是任何节点中的.next指针,并且您需要指针的地址来执行此操作.
| 归档时间: |
|
| 查看次数: |
35380 次 |
| 最近记录: |