管理 Objective c 中 NSError 的错误代码列表

发布于 2025-01-04 21:45:15 字数 274 浏览 2 评论 0原文

在 Cocoa 和 Objective C 中,最喜欢的管理错误的方法似乎是使用 NSError * 对象来构造错误对象,但是,我们需要调用以下方法

+ (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict

我的问题是,其中有哪些在整个应用程序中管理错误域、错误代码定义和用户信息字典的最佳实践,以便错误代码、域和用户信息字典始终保持一致?

In Cocoa and Objective C the favorite method for managing error seems to be using an NSError * object, to construct an error object however, we need to call the following method

+ (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict

My question is, what are some of the best practices for managing error domain, error code definitions and user info dictionary across the entire application so that error code, domain and user info dict always stays consistent?

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

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

发布评论

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

评论(2

心房的律动 2025-01-11 21:45:15

如果您有大量的错误构造,那么通过使用类您的生活可能会简单得多。我实际上使用 C++ 来实现此目的,因此可以删除程序不需要的调用(与 objc 不同),但您可以使用 C、ObjC 或 C++ 来实现此目的:

MONErrorDomain.h

// you won't normally need an instance here
@interface MONErrorDomain : NSObject

+ (NSString *)domain; // << required override
- (NSString *)domain; // << returns [[self class] domain]

// example convenience methods:
// uses [self domain]
+ (NSError *)errorWithErrorCode:(NSInteger)errorCode; // << user info would be nil
+ (NSError *)errorWithErrorCode:(NSInteger)errorCode userInfo:(NSDictionary *)userInfo;

@end

MONKoalaError。 h

@interface MONKoalaError : MONErrorDomain

+ (NSError *)outOfEucalyptus;

@end

extern NSString * const MONKoalaErrorDomain;

typedef enum MONKoalaErrorCode {
  MONKoalaErrorCode_Undefined = 0,
  MONKoalaErrorCode_OutOfEucalyptus
} MONKoalaErrorCode;

MONKoalaError.m

// apple recommends we use reverse domains
NSString * const MONKoalaErrorDomain = @"com.mon.koala-library.MONKoalaErrorDomain";

@implementation MONKoalaError

+ (NSString *)domain
{
  return MONKoalaErrorDomain;
}

+ (NSError *)outOfEucalyptus
{
  NSDictionary * info = …;
  return [self errorWithErrorCode:MONKoalaErrorCode_OutOfEucalyptus userInfo:info];
}

@end

然后,每个域的错误创建都在一个地方,客户端可以轻松地选择错误,而无需实际手动构建它们:

if (outError) {
  *outError = [MONKoalaError outOfEucalyptus];
}

并且错误处理采用以下形式:

if ([e.domain isEqualToString:MONKoalaErrorDomain]) {
  switch (e.code) {
    case MONKoalaErrorCode_OutOfEucalyptus : {
      self.needsEucalyptus = true;
…

If you have a hefty amount of error construction, your life could be much simpler by using a class. I actually use C++ for this so the calls a program does not need may be removed (unlike objc), but you can use C, ObjC, or C++ for this:

MONErrorDomain.h

// you won't normally need an instance here
@interface MONErrorDomain : NSObject

+ (NSString *)domain; // << required override
- (NSString *)domain; // << returns [[self class] domain]

// example convenience methods:
// uses [self domain]
+ (NSError *)errorWithErrorCode:(NSInteger)errorCode; // << user info would be nil
+ (NSError *)errorWithErrorCode:(NSInteger)errorCode userInfo:(NSDictionary *)userInfo;

@end

MONKoalaError.h

@interface MONKoalaError : MONErrorDomain

+ (NSError *)outOfEucalyptus;

@end

extern NSString * const MONKoalaErrorDomain;

typedef enum MONKoalaErrorCode {
  MONKoalaErrorCode_Undefined = 0,
  MONKoalaErrorCode_OutOfEucalyptus
} MONKoalaErrorCode;

MONKoalaError.m

// apple recommends we use reverse domains
NSString * const MONKoalaErrorDomain = @"com.mon.koala-library.MONKoalaErrorDomain";

@implementation MONKoalaError

+ (NSString *)domain
{
  return MONKoalaErrorDomain;
}

+ (NSError *)outOfEucalyptus
{
  NSDictionary * info = …;
  return [self errorWithErrorCode:MONKoalaErrorCode_OutOfEucalyptus userInfo:info];
}

@end

Then the error creation is all in one place for each domain, and the clients can easily pick their errors without actually building them manually:

if (outError) {
  *outError = [MONKoalaError outOfEucalyptus];
}

and error handling takes the form:

if ([e.domain isEqualToString:MONKoalaErrorDomain]) {
  switch (e.code) {
    case MONKoalaErrorCode_OutOfEucalyptus : {
      self.needsEucalyptus = true;
…
七色彩虹 2025-01-11 21:45:15

一种常见的方法是在头文件中定义一些适当的常量,然后在需要的地方包含该头文件。这是一种非常简单的方法,看起来像:

const NSString * kMyAppErrorDomain = @"com.example.myapp";
const NSInteger kMyAppSomeError = 2;

// key into user info dictionary
const NSString * kMyAppProblemKey = @"MyAppProblemKey";

我还看到一些应用程序创建了用于创建这些方法的便捷方法,或者作为 NSError 上的类别,或者作为单独的实用程序类或函数集。子类化 NSError 也是完全合理的,例如自定义本地化描述。

如果您还没有看到,Apple 已经发布了 错误处理编程指南讨论了如何在 Cocoa 中使用它们。

One common way is to define some appropriate constants in a header file, and then include that header file wherever needed. It's a pretty simple approach, and looks like:

const NSString * kMyAppErrorDomain = @"com.example.myapp";
const NSInteger kMyAppSomeError = 2;

// key into user info dictionary
const NSString * kMyAppProblemKey = @"MyAppProblemKey";

I've also seen some applications which create convenience methods for creating these, either as a category on NSError or as a separate utility class or set of functions. It's also entirely reasonable to subclass NSError, for example to customize the localized description.

If you have not already seen it, Apple has released the Error Handling Programming Guide which discusses how these should be used in Cocoa.

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