我如何从 swift 中的错误中获取键值
How can i fetch key value from Error in swift
如何以最佳方式从此错误结果中获取消息值。我尝试了很多但从未得到价值,所以请帮助我
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";
};
}
);
};
}})
为了访问错误的用户信息,您必须将 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"]
}
如何以最佳方式从此错误结果中获取消息值。我尝试了很多但从未得到价值,所以请帮助我
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";
};
}
);
};
}})
为了访问错误的用户信息,您必须将 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"]
}