objective-c 如何查看 NSError?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1559169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:20:55  来源:igfitidea点击:

How can I view an NSError?

iphoneobjective-ccocoalogging

提问by nevan king

What's the best way to log an NSError?

登录 的最佳方式是NSError什么?

- (void)checkThing:(Thing *)thing withError:(NSError *)error {
    NSLog(@"Error: %@", error);
}

Gives me a nullmessage

给我null发消息

回答by James Raybould

Looking at the NSErrordocumentation tells me that you'll need to do something like:

查看NSError文档告诉我,您需要执行以下操作:

NSLog(@"%@",[error localizedDescription]);

This should then give you human readable output

这应该会给你人类可读的输出

回答by Peter Hosey

NSLog(@"Error: %@", error);

Gives me a null message

NSLog(@"Error: %@", error);

给我一个空消息

Then erroris nil, not an NSError instance.

然后errornil,而不是 NSError 实例。

回答by Abizern

Here's a rough method I use to log errors while developing; (Not for Cocoa-touch)

这是我在开发时用来记录错误的粗略方法;(不适用于 Cocoa-touch)

// Execute the fetch request put the results into array
NSError *error = nil;
NSArray *resultArray = [moc executeFetchRequest:request error:&error];
if (resultArray == nil)
{
    // Diagnostic error handling
    NSAlert *anAlert = [NSAlert alertWithError:error];
    [anAlert runModal];
}

NSAlert takes care of displaying the error.

NSAlert 负责显示错误。