为什么是“错误:&错误”?这里使用(objective-c)
为什么这里使用“error:&error”(objective-c),
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
objective-c 中的对象无论如何都不会有效地传递引用吗?
why is "error:&error" used here (objective-c)
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
wouldn't an object in objective-c be effectively pass-by-reference anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
error:
的参数类型是NSError**
(即指向对象的指针的指针)。这允许moc
对象根据需要分配并初始化一个新的NSError
对象。这是一种常见的模式,尤其是在可可中。NSError 文档 给出了这种方法的动机的一些指示:
传入
NSError**
参数允许该方法返回任何有意义的NSError
子类。如果您传入NSError*
,则必须提供现有的NSError
对象,并且该方法无法返回不同的 来自您传入的对象。需要明确的是,该方法可能如下所示:
请注意,这还允许调用代码通过简单地为
错误传入
参数,如下:NULL
来忽略错误:更新:(回答问题):
参数类型必须为
NSError**
而不是NSError 有两个原因*
:1. 变量作用域规则,2. NSError 实例是不可变的。原因#1:变量作用域规则
让我们假设函数声明如下所示:
我们将这样调用函数:
当您以这种方式传递变量时,函数体将不会能够修改该变量的值(即函数体将无法创建新变量来替换现有变量)。例如,以下变量赋值仅存在于函数的局部范围内。调用代码仍然会看到
error == nil
。原因#2:NSError 的实例是不可变的
让我们保留相同的函数声明,但像这样调用函数:
首先,变量作用域规则保证
error
不会为nil
,因此if (error != nil) { ...
条件将始终为 true,但即使您想检查 < 中的特定错误信息code>if 块,你会运气不好,因为NSError
的实例是不可变的。这意味着一旦创建它们,您就无法修改它们的属性,因此该函数将无法更改该 NSError 的domain
或userInfo
> 您在调用代码中创建的实例。The argument type for
error:
isNSError**
(i.e. a pointer to a pointer to an object). This permits themoc
object to allocate and initialize a newNSError
object as required. It is a common pattern, especially in Cocoa.The NSError documentation gives some indication of the motivation for this approach:
Passing in an
NSError**
argument allows that method to return any subclass ofNSError
that makes sense. If you passed inNSError*
, you would have to supply an existingNSError
object, and there would be no way for the method to return a different object from the one you passed in.To be clear, the method could look something like this:
Note that this also allows the calling code to ignore errors by simply passing in
NULL
for theerror:
parameter, as follows:Update: (in response to questions):
There are two reasons why the argument type has to be
NSError**
instead ofNSError*
: 1. variable scoping rules, and 2. NSError instances are imutable.Reason #1: variable scoping rules
Let's assume that the function declaration were to look like this:
And we were to call the function like this:
When you pass in a variable this way, the function body will not be able to modify the value of that variable (i.e. the function body will not be able to create a new variable to replace the existing one). For example, the following variable assignments will exist only in the local scope of the function. The calling code will still see
error == nil
.Reason #2: instances of NSError are immutable
Let's keep the same function declaration, but call the function like this:
First of all, the variable scoping rules guarantee that
error
can not benil
, so theif (error != nil) { ...
condition will always be true, but even if you wanted to check for specific error information inside theif
block, you would be out of luck because instances ofNSError
are immutable. This means that once they are created, you cannot modify their properties, so the function would not be able to change thedomain
oruserInfo
of thatNSError
instance that you created in the calling code.它实际上是另一个返回值。按照 Cocoa 中的惯例,当操作有返回值时,错误并不占主导地位。当遇到错误时,可能会通过这个 out 参数返回给您。
对于
NSError
来说,它以这种方式工作,因为NSError
不是一个可变类型 - 它的字段在初始化时设置并且永远不会改变。因此,您不能像往常一样传递NSError
并设置错误代码。It's effectively another return value. The error is not dominant by convention in Cocoa when there is a return value for the operation. When an error is encountered, it may be returned to you by this out parameter.
In the case of
NSError
, it works this way becauseNSError
is not a mutable type - its fields are set at initialization and never mutated. Therefore, you cannot pass anNSError
as usual and set the error code.