Which fields does -[NSResponder presentError:] show?

Which fields does -[NSResponder presentError:] show?

是否有关于 -[NSResponder presentError:] 方法如何工作的文档?我正在尝试在我的库中创建 NSError 个实例,其 userInfo 值向用户显示尽可能多的有用信息。我过得很艰难。

我一开始只使用 NSLocalizedFailureReasonErrorKey。看到它分配了生成的 NSAlertmessageText 属性(粗体,最上面的标签),我接下来尝试添加 NSLocalizedDescriptionKey,我认为它会填写informativeText 属性,下面有一个不加粗的解释。我还添加了 NSURLErrorKey,因为这个库处理文件并且会很有帮助。

结果 NSAlert 根本不显示原始 NSLocalizedFailureReasonErrorKey 值,新的 NSLocalizedDescriptionKey 现在变成了 messageText。它也根本不公开 NSURLErrorKey

我唯一的选择是编写我自己的错误表示方法和特例这个库的域吗?

NSError 键如何与 presentError: 交互在 Localized Error Information 下的错误处理编程指南中有记录。

它包括一张图表,详细说明了哪些地方发生了什么。正如文档所述:

Failure reason

A brief sentence that explains the reason why the error occurred. It is typically part of the error description. Methods such as presentError: do not automatically display the failure reason because it is already included in the error description. The failure reason is for clients that only want to display the reason for the failure.

如果您想在呈现之前重新排列 NSError 对象中的字段,最好通过将 willPresentError: 覆盖为 return 调整后的错误对象来实现。

对于NSURLErrorKey这样的字段,如果要显示的话,希望在NSLocalizedDescriptionKeyNSLocalizedRecoverySuggestionErrorKey中包含。虽然它被称为“恢复建议”,但文档明确指出它可以“用作补充错误描述和失败原因的纯粹信息性消息”。