Alamofire Swift 5:传递参数时抛出错误
Posted
技术标签:
【中文标题】Alamofire Swift 5:传递参数时抛出错误【英文标题】:Alamofire Swift 5 : Throws an error while passing parameter 【发布时间】:2020-05-29 06:41:00 【问题描述】:我的参数值在控制台中打印时如下所示:
失败(Alamofire.AFError.responseSerializationFailed(原因: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误: 错误域=NSCocoaErrorDomain 代码=3840“周围的值无效 字符 0。” UserInfo=NSDebugDescription=无效值 字符 0.)))
["preparation_method[]": "[\"method_stage\":\"0\",\"method_description\":\"This is test methof1\"]", "recipe_id": "10454", "ingredients[]": "[\"ingredient_unit_id\":\"Cup\",\"ingredient_item_id\":\"1\",\"ingredient_item\":\"Rice\",\"ingredient_remark\":\"\",\"ingredient_qty\":\"1\",\"ingredient_unit_id\":\"\",\"ingredient_item_id\":\"2\",\"ingredient_item\":\"\",\"ingredient_remark\":\"\",\"ingredient_qty\":\"\",\"ingredient_unit_id\":\"\",\"ingredient_item_id\":\"3\",\"ingredient_item\":\"\",\"ingredient_remark\":\"\",\"ingredient_qty\":\"\"]"]
参数的格式如下所示:
["recipe_id" : "10454",
ingredients[]: ["ingredient_unit_id":"Cup","ingredient_item_id":"1","ingredient_item":"Rice","ingredient_remark":"","ingredient_qty":"1",
"ingredient_unit_id":"","ingredient_item_id":"2","ingredient_item":"","ingredient_remark":"","ingredient_qty":"",
"ingredient_unit_id":"","ingredient_item_id":"3","ingredient_item":"","ingredient_remark":"","ingredient_qty":""],
preparation_method[] :
["method_stage":"0","method_description":"This is test methof1"] ]
我使用的代码是
let arr = ingredient.map( Model(ingredient_item: $0,
ingredient_item_id: $1,
ingredient_qty: $2,
ingredient_unit_id: $3,
ingredient_remark: $4))
let jsonData = try! JSONEncoder().encode(arr)
let jsonString = String(data: jsonData, encoding: .utf8)
let ingredientstring = jsonString?.replacingOccurrences(of: "'\'" , with: "")
let ingredientarrayvalue = "\(ingredientstring!)"
print(ingredientarrayvalue)
let ingredient_array = String(ingredientarrayvalue)
let arr1 = prepatation.map( Model1(method_stage: $0,
method_description: $1))
let jsonData1 = try! JSONEncoder().encode(arr1)
let jsonString1 = String(data: jsonData1, encoding: .utf8)
let preparationstring = jsonString1?.replacingOccurrences(of: "'\'" , with: "")
let preparationarrayvalue = "\(preparationstring!)"
print(preparationarrayvalue)
let preparation_array = String(preparationarrayvalue)
let parameter = ["recipe_id" : recipevalue, "ingredients[]" : ingredient_array, "preparation_method[]" : preparationarrayvalue]
print(parameter)
let url = RECIPE.recipeSAVEURL
AF.request(url, method: .post, parameters: parameter).responseJSON(completionHandler: response in
print(response)
if let response_data = response.value as? NSDictionary
let responsestatus = response_data.value(forKey: "response") as! String
print(responsestatus)
if responsestatus == "success"
)
如何传递参数中没有“\”的参数?谁能帮我解决这个问题?
【问题讨论】:
【参考方案1】:在为我的上述问题寻找解决方案后,我发现了以下 github 链接:https://gist.github.com/cprovatas/5c9f51813bc784ef1d7fcbfb89de74fe
他们使用数据扩展的地方,我在我的代码中应用它如下:
let preparation_array = jsonString1!.data(using: .utf8)!.prettyPrintedJSONString!
let ingredient_array = jsonString!.data(using: .utf8)!.prettyPrintedJSONString!
上面提到的链接中使用的扩展代码的副本在下面给出以供参考,如果有的话:
extension Data
var prettyPrintedJSONString: NSString? /// NSString gives us a nice sanitized debugDescription
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else return nil
return prettyPrintedString
let str = "\"foo\": \"bar\"".data(using: .utf8)!.prettyPrintedJSONString!
debugPrint(str)
/* 打印: “富”:“酒吧”
【讨论】:
以上是关于Alamofire Swift 5:传递参数时抛出错误的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Alamofire 中上传文件并在参数中传递数据 - swift 5
Swift 5.1 和 Alamofire 5.1:GET 方法错误