失败,但不返回错误对象。 为什么?

发布于 2024-08-01 18:21:55 字数 752 浏览 3 评论 0原文

我试图理解 [NSData writeToFile:options:error:]。 该方法返回一个 BOOL,根据 Apple 的文档,该值是“如果操作成功则为 YES,否则为 NO”。

很公平,但如果它是“否”,我会假设错误参数将被设置为某个可检索的 NSError* 值。 但从我遇到的结果来看,情况并非如此。 因此我有点困惑,不知道如何确定导致失败的原因。

也就是说,我已经得到了这段代码(或多或少):

NSError* error = nil;
BOOL success = [data writeToFile: filePath error: &error];
if ( error )
    NSLog( @"error = %@", [error description] );

在我运行的代码中,success结果是NO,但是NSLog< /code> 语句永远不会被执行。 怎么会?

霍华德

I'm trying to understand the meaning of the value returned by [NSData writeToFile:options:error:]. The method returns a BOOL, which according to Apple's documentation is "YES if the operation succeeds, otherwise NO."

Fair enough, but if it's NO, I would have assumed that the error parameter would then be set to some retrievable NSError* value. However in results I'm coming across, that's not the case. Accordingly I'm somewhat confused, and don't know how to determine what caused the failure.

To wit, I've got this code (more or less):

NSError* error = nil;
BOOL success = [data writeToFile: filePath error: &error];
if ( error )
    NSLog( @"error = %@", [error description] );

success turns out to be NO in the code I'm running, but the NSLog statement is never executed. How come?

Howard

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖伴 2024-08-08 18:21:55

data 可能是 nil,在这种情况下 [data writeToFile:error:] 返回 nil,但是 < code>*error 未设置。

It's possible that data is nil, in which case [data writeToFile:error:] returns nil, but *error is not set.

維他命╮ 2024-08-08 18:21:55

writeToFile 方法在成功时返回 TRUE,在失败时返回 FALSE - 这就是您要实际检查的内容。

所以,尝试:

if(!success)

作为你的条件而不是 if( error )。

The writeToFile method returns TRUE on success and FALSE on failure -that's what you want to actually check for.

So, Try:

if(!success)

As your conditional instead of if( error ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文