本文介绍了什么是双星(如NSError **)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我看到这样的:

error:(NSError **)error

在苹果文档的。为什么两位明星?有什么意义?

in the apple doc's. Why two stars? What is the significance?

推荐答案

一个双星是一个指针的指针。因此, NSError ** 是一个指针的指针类型的对象 NSError 。基本上,它可以让你从函数返回一个错误的对象。你可以在你的函数创建一个指向 NSError 对象(称之为 * myError ),然后像做这样的:

A "double star" is a pointer to a pointer. So NSError ** is a pointer to a pointer to an object of type NSError. It basically allows you to return an error object from the function. You can create a pointer to an NSError object in your function (call it *myError), and then do something like this:

*error = myError;

回归的错误给调用者。

to "return" that error to the caller.

在下面贴回复评论:

您不能简单地使用 NSError * 因为在C,函数参数传递的通过值的,也就是说,这些值复制的时候传递给一个函数。为了说明这一点,考虑这个片段C $ C $的c:

You can't simply use an NSError * because in C, function parameters are passed by value—that is, the values are copied when passed to a function. To illustrate, consider this snippet of C code:

void f(int x)
{
    x = 4;
}

void g(void)
{
    int y = 10;
    f(y);
    printf("%d\n", y);    // Will output "10"
}

X f显示重新分配()不影响 F()(在克(),例如)。

The reassignment of x in f() does not affect the argument's value outside of f() (in g(), for example).

同样地,当一指针传递到一个函数,其值被复制,并重新分配,不会影响功能以外的值。

Likewise, when a pointer is passed into a function, its value is copied, and re-assigning will not affect the value outside of the function.

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!
}

当然,我们知道,我们能够改变的价值是什么以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"
}

所以,按道理说,要改变什么的 NSError * 点,我们也有一个指针传递给指针的值。

So it stands to reason that, to change the value of what an NSError * points to, we also have to pass a pointer to the pointer.

这篇关于什么是双星(如NSError **)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:34