如何在 Xcode 控制台中漂亮地打印 NSError 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7171775/
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
How to pretty-print an NSError object in Xcode console?
提问by Chaitanya Gupta
I printed an NSError object in the Xcode console (via NSLog(@"%@", error);
) and for a certain kind of error, this is what I get:
我在 Xcode 控制台中打印了一个 NSError 对象(通过NSLog(@"%@", error);
),对于某种错误,这就是我得到的:
Domain=NSCocoaErrorDomain Code=133020 "The operation couldn't be completed. (Cocoa error 133020.)" UserInfo=0xe939170 {conflictList=(
"NSMergeConflict (0xe93cad0) for NSManagedObject (0x5dba970) with objectID '0x5dc26f0 <x-coredata://775D53AE-58A4-4B18-BA52-D46781A183AE/SomeObject/p1>' with oldVersion = 2 and newVersion = 3 and old object snapshot = {\n creationDate = \"2011-08-24 06:52:22 +0000\";\n prop1 = \"a65e349a-b315-488e-b7f8-e459e353fd6e\";\n username = \"test-user\";\n password = \"foobar\";\n} and new cached row = {\n creationDate = \"2011-08-24 06:52:22 +0000\";\n prop1 = \"a65e349a-b315-488e-b7f8-e459e353fd6e\";\n username = \"test-user\";\n password = \"foobar\";\n}"
When I replace all the '\n's with newline and all the \"s with " in emacs, I get a much nicely formatted error message:
当我在 emacs 中将所有 '\n' 替换为换行符并将所有 \"s 替换为 " 时,我会收到一条格式非常好的错误消息:
Domain=NSCocoaErrorDomain Code=133020 "The operation couldn't be completed. (Cocoa error 133020.)" UserInfo=0xe939170 {conflictList=(
"NSMergeConflict (0xe93cad0) for NSManagedObject (0x5dba970) with objectID '0x5dc26f0 <x-coredata://775D53AE-58A4-4B18-BA52-D46781A183AE/SomeObject/p1>' with oldVersion = 2 and newVersion = 3 and old object snapshot = {
creationDate = "2011-08-24 06:52:22 +0000";
prop1 = "a65e349a-b315-488e-b7f8-e459e353fd6e";
username = "test-user";
password = "foobar";
} and new cached row = {
creationDate = "2011-08-24 06:52:22 +0000";
prop1 = "a65e349a-b315-488e-b7f8-e459e353fd6e";
username = "test-user";
password = "foobar";
}"
I would much prefer to see this nicely formatted error message in Xcode itself rather than copy-paste it and search-and-replace characters in another editor. Is there a way to do this?
我更喜欢在 Xcode 本身中看到这个格式很好的错误消息,而不是复制粘贴它并在另一个编辑器中搜索和替换字符。有没有办法做到这一点?
EDITFor clarity, the error is generated by a core data save operation:
编辑为了清楚起见,错误是由核心数据保存操作生成的:
NSError *error
if (![context save:&error]) {
NSLog(@"%@", error);
}
The offending part of the error object in this case (from where the \n's and \"s are being printed) is the value of the conflictList
key in the error's userInfo
dictionary.
在这种情况下,错误对象的有问题的部分(从 \n 和 \" 被打印的地方)是conflictList
错误userInfo
字典中键的值。
回答by superlogical
userInfo is a NSDictionary
userInfo 是一个 NSDictionary
NSLog(@" error => %@ ", [errorOrNil userInfo] )
Prints something like this for me
为我打印这样的东西
error => {
NSLocalizedDescription = "User already exists";
NSLocalizedFailureReason = "";
NSLocalizedRecoverySuggestion = "Retry request based on information in `NSLocalizedFailureReasonErrorKey`";
kinveyErrorCode = UserAlreadyExists;
kinveyInternalErrorString = "";
kinveyRequestId = e5be0aed155e4925b3365d57de3dc5b2;
}
You can also try:
你也可以试试:
NSLog(@" error => %@ ", [errorOrNil localizedDescription] )
Which prints out:
打印出来:
You got an error: User already exists
回答by d.lebedev
Not a very cool solution - you can write your own category for the NSError class and represent the text as you want.
不是一个很酷的解决方案 - 您可以为 NSError 类编写自己的类别并根据需要表示文本。