LoginSignup
28
26

More than 5 years have passed since last update.

NSErrorによるエラー判定実装方法

Last updated at Posted at 2013-12-16
//エラーの発生する可能性のあるメソッド
- (BOOL) setCategory:(NSString*)theCategory error:(NSError**)outError {
    //処理
    if (outError) {
        *outError = [NSError errorWithDomain:@"ドメイン" code:-1 userInfo:nil];
        return NO;
    }
    return YES;
}

//呼び出し側
NSError *error = nil;
BOOL success = [self setCategory:@"TEST" error:&error];

//エラー発生確認
//メソッドから処理結果判定が行えない場合はif(error)により判定を行う
if (!success) {
    NSLog(@"%@ %ld %@",[error domain],(long)[error code],[[error userInfo] description]);
}

&errorでNSErrorのポインタを渡してメソッド側でNSErrorオブジェクトを生成
その後メソッドの戻り値(またはNSErrorオブジェクトの有無)によりエラー内容を判定する。

28
26
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
26