【发布时间】:2020-05-27 17:25:53
【问题描述】:
我被困住了,我肯定是个愚蠢的东西。
如果我从 API 收到错误,我想显示一个错误。
在下面的示例中,我有 2 个错误,所以我想在我的警报消息中显示 2 行。
这是我从 Alamofire 收到的:
let swiftyJsonVar = JSON(response.result.value!):
▿ {
"errors" : {
"tva" : [
"Tax must be numeric."
],
"data_id" : [
"Data id must exist"
]
},
"message" : "The given data was invalid."
}
- rawArray : 0 elements
▿ rawDictionary : 2 elements
▿ 0 : 2 elements
- key : "errors"
▿ value : 2 elements
▿ 0 : 2 elements
- key : "tva"
▿ value : 1 element
- 0 : Tax must be numeric.
▿ 1 : 2 elements
- key : "data_id"
▿ value : 1 element
- 0 : Data id must exist
▿ 1 : 2 elements
- key : "message"
- value : The given data was invalid.
- rawString : ""
- rawNumber : 0
- rawNull : <null>
- rawBool : false
- type : SwiftyJSON.Type.dictionary
- error : nil
这是我尝试过的:
SharedClass.sharedInstance.alertTitle(view: self, title: "Error".localized, message: "\(swiftyJsonVar["errors"].description)")
但它显示这个:
"{\n \"errors\" : {\n \"tva\" : [\n \"Tax must be numeric.\"\n ],\n \"data_id\" : [\n \"Data id must exist\"\n ]\n },\n \"message\" : \"The given data was invalid.\"\n}"
我想在这种情况下有一些东西它应该显示在 2 行:
tva: Tax must be numeric.
data_id: Data id must exist
所以它会根据收到的错误数显示 n 行。
感谢您的帮助
【问题讨论】:
-
显示您的代码以获取响应。
-
当 5.0 已经推出一年多并且当前版本是 5.2 时,您是否有理由指定 Swift 4?
-
description 用于调试,仅此而已。
标签: swift xcode alamofire uialertcontroller