我该如何在我的iPhone应用中使用NSError?

236

我正在解决我的应用程序中的错误,考虑使用NSError。但我对如何使用它以及如何填充它有些困惑。

请问是否能提供一个示例,展示如何填充并使用NSError

9个回答

486

好的,通常我会使可能在运行时出现错误的方法接受一个指向 NSError 指针的引用。如果确实发生错误,我可以将 NSError 引用填充为错误数据并从该方法返回 nil。

示例:

- (id) endWorldHunger:(id)largeAmountsOfMonies error:(NSError**)error {
    // begin feeding the world's children...
    // it's all going well until....
    if (ohNoImOutOfMonies) {
        // sad, we can't solve world hunger, but we can let people know what went wrong!
        // init dictionary to be used to populate error object
        NSMutableDictionary* details = [NSMutableDictionary dictionary];
        [details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];
        // populate the error object with the details
        *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
        // we couldn't feed the world's children...return nil..sniffle...sniffle
        return nil;
    }
    // wohoo! We fed the world's children. The world is now in lots of debt. But who cares? 
    return YES;
}

我们可以这样使用该方法。除非该方法返回nil,否则不要浪费时间检查错误对象:
// initialize NSError object
NSError* error = nil;
// try to feed the world
id yayOrNay = [self endWorldHunger:smallAmountsOfMonies error:&error];
if (!yayOrNay) {
   // inspect error
   NSLog(@"%@", [error localizedDescription]);
}
// otherwise the world has been fed. Wow, your code must rock.

我们能够访问错误的“localizedDescription”,因为我们为“NSLocalizedDescriptionKey”设置了一个值。
获取更多信息的最佳地方是苹果文档。它真的很好。
还有一个关于Cocoa Is My Girlfriend的不错、简单的教程。

40
这是有史以来最有趣的例子。 - meow
这是一个相当棒的答案,尽管在ARC和将id转换为BOOL时存在一些问题。如果有任何轻微的ARC兼容变化,将不胜感激。 - NSTJ
8
如果因为苹果推动我们转向新的ARC世界,而导致我们无法结束全球饥饿问题,我会非常恼火。 - Manav
1
返回类型可以是 BOOL。如果出现错误,请返回 NO,而不是检查返回值,只需检查 error。如果为 nil,则继续执行,如果 != nil,则处理它。 - Gabriele Petronella
8
你需要加入代码来验证 **error 不是 nil。否则程序会抛出一个完全不友好并且不明确发生了什么的错误。 - FreeAsInBeer
我猜Alex已经写了生产代码,以后他会因为自己的注释而笑...懂那种感觉。 - CularBytes

60

根据我最近的实现,我想提供一些更多的建议。我研究了Apple的一些代码,我认为我的代码行为方式基本一致。

上面的帖子已经解释了如何创建NSError对象并返回它们,所以我不会在这个部分浪费时间。我只是尝试建议一种将错误(代码、消息)集成到您自己的应用程序中的好方法。


我建议创建一个头文件,该头文件将概述您域中所有错误(即应用程序、库等)。我的当前头文件看起来像这样:

FSError.h

FOUNDATION_EXPORT NSString *const FSMyAppErrorDomain;

enum {
    FSUserNotLoggedInError = 1000,
    FSUserLogoutFailedError,
    FSProfileParsingFailedError,
    FSProfileBadLoginError,
    FSFNIDParsingFailedError,
};

FSError.m

#import "FSError.h" 

NSString *const FSMyAppErrorDomain = @"com.felis.myapp";

现在当您使用以上错误值时,苹果将为您的应用程序创建一些基本的标准错误信息。错误消息可能会像以下示例创建:
+ (FSProfileInfo *)profileInfoWithData:(NSData *)data error:(NSError **)error
{
    FSProfileInfo *profileInfo = [[FSProfileInfo alloc] init];
    if (profileInfo)
    {
        /* ... lots of parsing code here ... */

        if (profileInfo.username == nil)
        {
            *error = [NSError errorWithDomain:FSMyAppErrorDomain code:FSProfileParsingFailedError userInfo:nil];            
            return nil;
        }
    }
    return profileInfo;
}

上述代码的标准Apple生成的错误信息(error.localizedDescription)如下所示:

Error Domain=com.felis.myapp Code=1002 "无法完成操作。(com.felis.myapp错误1002。)"

对于开发人员来说,上面的信息已经非常有用,因为该消息显示了发生错误的域和相应的错误代码。但是,最终用户不知道错误代码 1002 的含义,因此现在我们需要为每个代码实现一些好的消息。
在编写错误消息时,我们必须牢记本地化(即使我们不立即实现本地化消息)。在我的当前项目中,我使用了以下方法:
1)创建一个包含错误的strings文件。字符串文件很容易本地化。该文件可能看起来像下面这样:

FSError.strings

"1000" = "User not logged in.";
"1001" = "Logout failed.";
"1002" = "Parser failed.";
"1003" = "Incorrect username or password.";
"1004" = "Failed to parse FNID."

2) 添加宏,将整数代码转换为本地化错误消息。我在我的Constants+Macros.h文件中使用了2个宏。为方便起见,我总是将此文件包含在前缀头文件 (MyApp-Prefix.pch) 中。

Constants+Macros.h

// error handling ...

#define FS_ERROR_KEY(code)                    [NSString stringWithFormat:@"%d", code]
#define FS_ERROR_LOCALIZED_DESCRIPTION(code)  NSLocalizedStringFromTable(FS_ERROR_KEY(code), @"FSError", nil)

3) 现在基于错误代码展示用户友好的错误信息变得更加容易,例如:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
            message:FS_ERROR_LOCALIZED_DESCRIPTION(error.code) 
            delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil];
[alert show];

9
好的!但是为什么不把本地化描述放在用户信息字典中呢?[NSError errorWithDomain:FSMyAppErrorDomain code:FSProfileParsingFailedError userInfo:@{NSLocalizedDescriptionKey : FS_ERROR_LOCALIZED_DESCRIPTION(error.code)}]; - Richard Venable
1
我应该把字符串文件放在哪个特定的位置?从FS_ERROR_LOCALIZED_DESCRIPTION()中,我只得到了数字(错误代码)。 - huggie
@huggie:不太确定你的意思。我通常会把整个应用程序中使用的这些宏放在一个名为“Constants+Macros.h”的文件中,并在前缀头(.pch文件)中导入此文件,以便在任何地方都可以使用。如果您的意思是您只使用了其中的1个宏,那可能是可行的。也许将int转换为NSString并不是真正必要的,尽管我还没有测试过。 - Wolfgang Schreurs
@huggie:哦,我现在明白你的意思了。字符串应该放在本地化文件(.strings 文件)中,因为这是苹果宏查找的位置。在这里阅读有关使用 NSLocalizedStringFromTable 的信息:https://developer.apple.com/library/mac/documentation/cocoa/conceptual/loadingresources/Strings/Strings.html - Wolfgang Schreurs
1
@huggie:是的,我使用了本地化字符串表。宏FS_ERROR_LOCALIZED_DESCRIPTION中的代码检查名为FSError.strings的文件中的可本地化字符串。如果您对此不熟悉,您可能需要查看苹果的本地化指南.strings文件。 - Wolfgang Schreurs
显示剩余2条评论

38

亚历克斯,你的回答很好。潜在的问题是空指针引用。苹果关于创建和返回NSError对象的参考资料。

...
[details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];

if (error != NULL) {
    // populate the error object with the details
    *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
}
// we couldn't feed the world's children...return nil..sniffle...sniffle
return nil;
...

32

Objective-C

NSError *err = [NSError errorWithDomain:@"some_domain"
                                   code:100
                               userInfo:@{
                                           NSLocalizedDescriptionKey:@"Something went wrong"
                               }];

Swift 3

let error = NSError(domain: "some_domain",
                      code: 100,
                  userInfo: [NSLocalizedDescriptionKey: "Something went wrong"])

9
请参考以下 教程 我希望这对你有所帮助,但是在此之前,您需要阅读 NSError 的文档。
我最近发现了这个非常有趣的链接 错误处理

3

我见过的另一种设计模式涉及使用块,特别是在方法异步运行时非常有用。

假设我们已经定义了以下错误代码:

typedef NS_ENUM(NSInteger, MyErrorCodes) {
    MyErrorCodesEmptyString = 500,
    MyErrorCodesInvalidURL,
    MyErrorCodesUnableToReachHost,
};

您需要定义一个能够引发错误的方法,如下所示:
- (void)getContentsOfURL:(NSString *)path success:(void(^)(NSString *html))success failure:(void(^)(NSError *error))failure {
    if (path.length == 0) {
        if (failure) {
            failure([NSError errorWithDomain:@"com.example" code:MyErrorCodesEmptyString userInfo:nil]);
        }
        return;
    }

    NSString *htmlContents = @"";

    // Exercise for the reader: get the contents at that URL or raise another error.

    if (success) {
        success(htmlContents);
    }
}

然后,当您调用它时,您不需要担心声明NSError对象(代码补全将为您完成),也不需要检查返回值。您只需提供两个块:一个在发生异常时被调用,另一个在成功时被调用:

[self getContentsOfURL:@"http://google.com" success:^(NSString *html) {
    NSLog(@"Contents: %@", html);
} failure:^(NSError *error) {
    NSLog(@"Failed to get contents: %@", error);
    if (error.code == MyErrorCodesEmptyString) { // make sure to check the domain too
        NSLog(@"You must provide a non-empty string");
    }
}];

3

我将尝试总结Alex和jlmendezbonini的精彩回答,并添加一个修改,使所有东西都兼容ARC(目前还不是这样,因为ARC会抱怨,因为您应该返回id,这意味着“任何对象”,但BOOL不是对象类型)。

- (BOOL) endWorldHunger:(id)largeAmountsOfMonies error:(NSError**)error {
    // begin feeding the world's children...
    // it's all going well until....
    if (ohNoImOutOfMonies) {
        // sad, we can't solve world hunger, but we can let people know what went wrong!
        // init dictionary to be used to populate error object
        NSMutableDictionary* details = [NSMutableDictionary dictionary];
        [details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];
        // populate the error object with the details
        if (error != NULL) {
             // populate the error object with the details
             *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
        }
        // we couldn't feed the world's children...return nil..sniffle...sniffle
        return NO;
    }
    // wohoo! We fed the world's children. The world is now in lots of debt. But who cares? 
    return YES;
}

现在,我们不再检查方法调用的返回值,而是检查error是否仍为nil。如果不是,我们就有问题了。
// initialize NSError object
NSError* error = nil;
// try to feed the world
BOOL success = [self endWorldHunger:smallAmountsOfMonies error:&error];
if (!success) {
   // inspect error
   NSLog(@"%@", [error localizedDescription]);
}
// otherwise the world has been fed. Wow, your code must rock.

3
苹果公司建议使用间接变量返回错误时,方法本身应始终具有成功或失败的某些返回值。苹果公司敦促开发人员首先检查返回值,仅当返回值不合法时才检查错误。请参阅以下页面:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html - Wolfgang Schreurs

1
extension NSError {
    static func defaultError() -> NSError {
        return NSError(domain: "com.app.error.domain", code: 0, userInfo: [NSLocalizedDescriptionKey: "Something went wrong."])
    }
}

每当我没有有效的错误对象时,我可以使用NSError.defaultError()

let error = NSError.defaultError()
print(error.localizedDescription) //Something went wrong.

0

虽然有点超出问题范围,但如果您没有NSError选项,您总是可以显示低级错误:

 NSLog(@"Error = %@ ",[NSString stringWithUTF8String:strerror(errno)]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接