我在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}"
当我在emacs中用newline替换所有的'\n',用"替换所有的\"时,我得到了一个格式更好的错误信息:
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";
}"
我更希望在Xcode中看到这个格式良好的错误信息,而不是将其复制粘贴并在另一个编辑器中搜索和替换字符。有没有办法做到这一点?
编辑 为了清楚起见,该错误是由Core Data保存操作生成的:
NSError *error
if (![context save:&error]) {
NSLog(@"%@", error);
}
在这种情况下,错误对象中有问题的部分(打印出换行符和引号的部分)是错误的
userInfo
字典中conflictList
键的值。请注意保留HTML标签。
NSLog(@"%@", [error userInfo]);
或者po [error userInfo]
吗? - jtbandesuserInfo
。得到的输出与之前的一样,只是没有打印出错误的域和代码。 - Chaitanya Guptapo [error userInfo]
是什么意思? - Chaitanya Guptapo [error userInfo]
是调试器打印出userInfo字典的命令,类似于NSLog
语句。 - jtbandes