gpt4 book ai didi

swift - 我如何快速从错误中获取键值

转载 作者:搜寻专家 更新时间:2023-11-01 06:17:00 25 4
gpt4 key购买 nike

如何以最佳方式从此错误结果中获取消息值。我尝试了很多但从未得到值(value),所以请帮助我

 BUYClient.sharedClient.createCustomer(with: credentials) { (customer : BUYCustomer?,token:  BUYCustomerToken?, error :Error?) -> Void in
if customer != nil{
print(token ?? "kuch nahi aaya")
print(customer?.fullName ?? "kuch nahi aaya")
}
let customer = error.debugDescription

print("value===\(customer)")
}

结果:

     value===Optional(Error Domain=BUYShopifyErrorDomain Code=422 "(null)" UserInfo={errors={
customer = {
email = (
{
code = taken;
message = "has already been taken";
options = {
"rescue_from_duplicate" = 1;
value = "y@gmail.com";
};
}
);
};
}})

最佳答案

为了访问错误的 userInfo,您必须将 Error 转换为经典的 NSError,之后您可以访问 userInfo 就像一本字典。

let nserror = error as! NSError
if let errors = nserror.userInfo["errors"] as? [String: NSObject],
let customer = errors["customer"] as? [String: NSObject],
let email = customer["email"] as? [[String: NSObject]]
{
let message = email[0]["message"]
}

关于swift - 我如何快速从错误中获取键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42506042/

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