NSError域/自定义域 - 约定和最佳做法 [英] NSError domains / custom domains - conventions and best practices

查看:181
本文介绍了NSError域/自定义域 - 约定和最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



NSError需要一个域,我理解它会分割错误代码的范围。一个人会预期有一个注册表的domain.error代码,但我'



假设这可以用于查找本地化的错误描述。



任何人都有任何一套已知的最佳实践来处理错误域和代码?



在您的项目中,您需要维护您的错误域/注册表的注册表,基于域/代码映射到本地化描述,恢复,众所周知的用户信息键或类似工厂的错误对象的代码?



或者你一般只是拼凑一些域和一些代码,并依赖于众所周知的userinfo键,如NSLocalizedDescriptionKey等,在您的定制NSError域?

解决方案

Apple通常会在名为FrameworkNameErrors.h的标题中发布其NS / CFError代码。例如,Foundation的错误代码在Foundation / FoundationErrors.h中。此外,他们通常在框架的文档中发布域和代码。



任何具有自己的错误域和代码的框架的独立作者都应该这样做。


NSError requires a domain, which I understand segments the range of error codes.

One would anticipate that there exist somewhere a registry of domain.error code but I've not been able to discover one.

Supposedly this could used for looking up localized descriptions for errors.

Does anyone have any set of known best practices for dealing with error domains and codes? An authoritative reference (major developers or framework makers) is optimal, but even blogs detailing a good convention is useful.

In your projects do you maintain registries of your error domains/codes that map to localized descriptions, recoveries, well known userinfo keys or like factories for your error objects based on domain/code?

Or do you generally just cobble together some domain and some code, and depend on well known userinfo keys such as NSLocalizedDescriptionKey, etc, in your bespoke NSError domains?

解决方案

Apple generally publishes their NS/CFError codes in a header named FrameworkNameErrors.h. For example, Foundation's error codes are in Foundation/FoundationErrors.h. In addition, they generally publish the domain and codes in their documentation for the framework.

Any independent author of a framework with its own error domain and codes should do the same.

这篇关于NSError域/自定义域 - 约定和最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆