gpt4 book ai didi

ios - 如何在 Xcode 控制台中漂亮地打印 NSError 对象?

转载 作者:可可西里 更新时间:2023-11-01 03:08:40 26 4
gpt4 key购买 nike

我在 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 中将所有的 '\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";
}"

我更愿意在 Xcode 本身中看到这个格式良好的错误消息,而不是复制粘贴它并在另一个编辑器中搜索和替换字符。有办法做到这一点吗?

编辑 为了清楚起见,错误是由核心数据保存操作产生的:

NSError *error
if (![context save:&error]) {
NSLog(@"%@", error);
}

在这种情况下,错误对象的违规部分(从打印\n 和\"s 的地方)是错误的 userInfo conflictList 键的值字典。

最佳答案

userInfo 是一个 NSDictionary

 NSLog(@" error => %@ ", [errorOrNil userInfo] )

为我打印这样的东西

error => {
NSLocalizedDescription = "User already exists";
NSLocalizedFailureReason = "";
NSLocalizedRecoverySuggestion = "Retry request based on information in `NSLocalizedFailureReasonErrorKey`";
kinveyErrorCode = UserAlreadyExists;
kinveyInternalErrorString = "";
kinveyRequestId = e5be0aed155e4925b3365d57de3dc5b2;
}

你也可以试试:

 NSLog(@" error => %@ ", [errorOrNil localizedDescription] )

打印出:

You got an error: User already exists 

关于ios - 如何在 Xcode 控制台中漂亮地打印 NSError 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7171775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com