私は自分のアプリでエラーをキャッチすることに取り組んでおり、を使用することを検討していNSError
ます。私はそれをどのように使用するか、そしてそれをどのように移入するかについて少し混乱しています。
誰かが私がどのようにデータを入力してから使用するかについての例を提供できますNSError
か?
私は自分のアプリでエラーをキャッチすることに取り組んでおり、を使用することを検討してい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
) に含めます。
定数+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];
素晴らしい答えアレックス。潜在的な問題の 1 つは、NULL 逆参照です。NSError オブジェクトの作成と返却に関する Apple のリファレンス
...
[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;
...
Objective-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"])
アレックスとjlmendezboniniのポイントによる素晴らしい答えを要約して、すべてを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.
それは少し問題の範囲外ですが、 NSError のオプションがない場合は、いつでも低レベルのエラーを表示できます:
NSLog(@"Error = %@ ",[NSString stringWithUTF8String:strerror(errno)]);