如何从NSError **获取NSError?

时间:2019-11-22 13:59:08

标签: objective-c

我有一个类型为NSError**的错误变量,并且我想获取错误的error.description字段。有没有办法将其转换为NSError?

1 个答案:

答案 0 :(得分:2)

NSError **是指向NSError *的指针。要访问基础的NSError *,请用*取消引用。但是,只有在间接指针不为NULL的情况下,这才是合法的。

if (error != NULL) {
    NSString *desc = [*error description];
    ...
}

对此进行更具体的说明:

NSError **error = NULL; // Pointer to NULL
[*error description];   // Invalid and will crash.

NSError *underlyingError = nil;
[underlyingError description];      // This is fine and just returns nil
NSError **error = &underlyingError; // Pointer to a pointer
[*error description];               // This is fine and just returns nil
相关问题