0

iOS 6.1に対処するコードをいくつか開発しましたNSError。しかし、私はそれに満足していません。それはせいぜいハックです:

 -(bool) reptErrAtModule: (NSString *) module
                  atSubr: (NSString *) subr
                  atFunc: (NSString *) func
                 withErr: (NSError *) err
   {
   id value = [[err userInfo] objectForKey: NSUnderlyingErrorKey];
   NSString * errDesc = (value != nil) ? 
                        [value localizedDescription]:
                        (NSString *)[[err userInfo] objectForKey: @"reason"];

   NSLog( @"ERR -> %@",[NSString stringWithFormat: 
          @"(%@>%@) %@ failed! %@",module,subr,func,errDesc] );
   }

より単純なフォーム ((NSString *)[[err userInfo] objectForKey: @"reason"]大文字と小文字を区別しない) があり、 への呼び出しから返されたエラーに対して機能しましたremoveItemAtPath

しかし、その後、このコードからエラーが返されました:

NSPersistentStore * entStor = 
 [myPerStoCor addPersistentStoreWithType: NSSQLiteStoreType
                           configuration: nil
                                     URL: [NSURL fileURLWithPath: Path]
                                 options: nil
                                   error: &err];

そして、私のルーチンはエラーを抽出できませんでした。@"reason"デバッガーの Info データに必要なテキストが表示されたので、ロジックを追加しました。

現在、コードは両方のタイプのエラーで機能しますが、これはこれを行う方法ではないと考えています。システムが返す可能性のあるすべてのタイプのエラーに対処するための、より優れた、より一般的な方法が必要ですNSError

4

2 に答える 2

0

私はこれを使用します:

NSString *description = error.localizedDescription;
NSString *reason = error.localizedFailureReason;
NSString *errorMessage = [NSString stringWithFormat:@"%@ %@", description, reason];
于 2013-09-05T11:56:00.423 に答える