Why is `&` (ampersand) put in front of some method parameters?

后端 未结 3 997
面向向阳花
面向向阳花 2020-12-23 20:49

I\'ver wondered, why is it that in front of an NSError, such as below, do we put: &error and not error?

E.g.

NSArray *r         


        
3条回答
  •  时光说笑
    2020-12-23 21:01

    The error parameter's type is (NSError **), that is a pointer to a pointer to an NSError. The error variable that you use as the argument is probably declared as NSError *, so in order to get the types to match properly, you have to use the address of operator to get a pointer to a pointer (&error). The reason the method needs a pointer to a pointer in the first place is so that it can modify the value of error and have that new value be available to you, the caller of the method.

提交回复
热议问题