为什么`&`(&符号)放在一些方法参数前面? [英] Why is `&` (ampersand) put in front of some method parameters?

查看:135
本文介绍了为什么`&`(&符号)放在一些方法参数前面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,为什么在NSError之前,如下所示,我们放了:& error 而不是错误

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

例如

NSArray *result = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

希望你能解释,这也总是这样或只在某些情况下才需要这样做?谢谢。

Hope you can explain, also is this always the way or only in certain situations this is needed? Thanks.

推荐答案

您需要获取错误的地址,因为功能需要修改它。 错误通过指针传递,因此您需要获取地址运算符& 对于它。

You need to take the address of error because the function needs to modify it. error is passed by pointer, so you need the "take address" operator & for it.

C和Objective-C按值传递参数。如果您传递错误而没有&符号,并且您调用的方法会修改它,那么调用该函数的函数将看不到任何更改,因为该方法将在其本地副本 NSError *

C and Objective-C pass parameters by value. If you pass error without an ampersand and the method that you call modifies it, your function that made the call would not see any changes, because the method would operate on its local copy of NSError*.

你知道在前面需要一个&符号相应的参数如果你看一下方法的签名并看到 ** 那里:

You know that you need an ampersand in front of the corresponding parameter if you look at the signature of the method and see ** there:

- (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error
                                            //   ^ one                    ^^ two

这篇关于为什么`&`(&符号)放在一些方法参数前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆