Ios decodeTopLevelObjectOfClass取消归档的目的是什么

Ios decodeTopLevelObjectOfClass取消归档的目的是什么,ios,objective-c,Ios,Objective C,为什么我们应该使用decodeTopLevelObjectOfClass而不是decodeObjectOfClass?在我的例子中,decodeObjectOfClass在嵌套的自定义对象归档中不起作用。这与-[NSCoder error]有关 来自的NSCoder.h中的标题注释-failWithError:: Sets an error on this NSCoder once per TopLevel decode; calling it repeatedly will have no e

为什么我们应该使用decodeTopLevelObjectOfClass而不是decodeObjectOfClass?在我的例子中,decodeObjectOfClass在嵌套的自定义对象归档中不起作用。

这与
-[NSCoder error]
有关

来自
NSCoder.h
中的标题注释-failWithError:

Sets an error on this NSCoder once per TopLevel decode; calling it
repeatedly will have no effect until the call stack unwinds to one
of the TopLevel decode entry-points.

This method is only meaningful to call for decodes.

Typically, you would want to call this method in your -initWithCoder:
implementation when you detect situations like:
 - lack of secure coding
 - corruption of your data
 - domain validation failures

After calling -failWithError: within your -initWithCoder:
implementation, you should clean up and return nil as early as possible.

Once an error has been signaled to a decoder, it remains set until
it has handed off to the first TopLevel decode invocation above it.
For example, consider the following call graph:
 A    -decodeTopLevelObjectForKey:error:
 B        -initWithCoder:
 C            -decodeObjectForKey:
 D                -initWithCoder:
 E                    -decodeObjectForKey:
 F                        -failWithError:

In this case the error provided in stack-frame F will be returned
via the outError in stack-frame A. Furthermore the result object
from decodeTopLevelObjectForKey:error: will be nil, regardless of
the result of stack-frame B.

NSCoder implementations support two mechanisms for the stack-unwinding from F to A:
 - forced (NSException based)
 - particpatory (error based)

The kind of unwinding you get is determined by the decodingFailurePolicy
property of this NSCoder (which defaults to NSDecodingFailurePolicyRaiseException
to match historical behavior).

对于
-错误

While .error is non-nil, all attempts to decode data from this
coder will return a nil/zero-equivalent value.

This error is consumed by a TopLevel decode API (which resets
this coder back to a being able to potentially decode data).

“使用过NSKeyedArchiver等NSCODER的人都知道,这些东西没有错误参数。我们添加了显式错误处理。像这样的方法,为键解码对象和为键解码类对象,现在有了实际返回错误的版本,以Swift抛出错误。它们被命名为解码顶级Object代表键,解码类的顶级对象代表键。请注意,它们有抛出声明,这意味着它们返回了一个错误。“也许值得一试观看该会话。@Larme是的,我看到decodeTopLevelObjectOfClass添加了新的错误参数,它还有其他优势吗?在我的例子中,我想归档嵌套的自定义对象,因为它不适用于decodeObjectOfClass。但它在类的DecodeToplevelObjects中运行良好。