簡體   English   中英

如何在我的 iPhone 應用程序中使用 NSError?

[英]How can I use NSError in my iPhone App?

我正在努力捕捉我的應用程序中的錯誤,我正在考慮使用NSError 我對如何使用它以及如何填充它感到有些困惑。

有人可以提供一個關於我如何填充然后使用NSError的例子嗎?

好吧,我通常做的是讓我的方法可以在運行時出錯,引用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設置了一個值。

獲取更多信息的最佳位置是Apple 的文檔 這真的很好。

Cocoa Is My Girlfriend上還有一個不錯的簡單教程。

我想根據我最近的實施添加更多建議。 我看過一些來自 Apple 的代碼,我認為我的代碼的行為方式大致相同。

上面的帖子已經解釋了如何創建 NSError 對象並返回它們,所以我不會打擾那部分。 我只是嘗試提出一種在您自己的應用程序中集成錯誤(代碼、消息)的好方法。


我建議創建 1 個標頭,用於概述您的域(即應用程序、庫等)的所有錯誤。 我當前的標題如下所示:

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";

現在,當使用上述錯誤值時,Apple 將為您的應用程序創建一些基本的標准錯誤消息。 可能會產生如下錯誤:

+ (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 "The operation couldn't be completed. (com.felis.myapp error 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 ) 中。

常量+宏.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];

很好的答案亞歷克斯。 一個潛在的問題是 NULL 取消引用。 Apple 關於創建和返回 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;
...

目標-C

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

斯威夫特 3

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

請參考以下教程

我希望它對你有幫助,但在你必須閱讀NSError 的文檔之前

這是我最近發現的非常有趣的鏈接ErrorHandling

我將嘗試總結 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.

我見過的另一種設計模式涉及使用塊,這在異步運行方法時特別有用。

假設我們定義了以下錯誤代碼:

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");
    }
}];
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.

好吧,這有點超出問題范圍,但如果您沒有 NSError 選項,您始終可以顯示低級錯誤:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM