Ios Alamofire Swift 5:传递参数时引发错误
我的参数值在控制台中打印时如下所示,因此我得到一个错误: 失败(Alamofire.AFError.responseSerializationFailed(原因: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailureReason(错误: 错误域=NSCOCAERRORDOMAIN Code=3840“周围的值无效 字符0。“UserInfo={NSDebugDescription=周围的值无效。” 字符(0.}) 参数的格式如下所示:Ios Alamofire Swift 5:传递参数时引发错误,ios,swift,parameter-passing,alamofire,Ios,Swift,Parameter Passing,Alamofire,我的参数值在控制台中打印时如下所示,因此我得到一个错误: 失败(Alamofire.AFError.responseSerializationFailed(原因: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailureReason(错误: 错误域=NSCOCAERRORDOMAIN Code=3840“周围的值无效 字符0。“UserInfo={NSDebugDescription=周围的值无效。
["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"{
}
}
})
我如何在参数中没有“\”的情况下传递参数?有人能帮我吗?在为我的上述问题搜索了数小时的解决方案后,我遇到了以下github链接: 他们对数据使用扩展,我在代码中应用了它,如下所示:
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)
/*印刷品:
{
“foo”:“bar”
}在花了数小时寻找上述问题的解决方案之后,我发现了以下github链接: 他们对数据使用扩展,我在代码中应用了它,如下所示:
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)
/*印刷品:
{
“foo”:“bar”
}