我有这样的模型类
class Example() { var name:String? var age:String? var marks:String? }
我正在向该模型类添加数据
let example = Example() example.name = "ABC" example.age = "10" example.marks = "10"
之后我转换为JSON然后我发布了
Alamofire.request(URL,method:.post,parameters: example)
Alamofire不接受参数只接受类似参数= [“”:“”,“”,“”] – >基于键值,所以我试图将模型转换为JSON,JSON转换为字典,即使不接受它的显示喜欢参数问题.我需要总模型对象需要在Alamofire中作为post方法的参数发送,如下所示:
let example = Example() Alamofire.request(URL,parameters: example)
由于Alamofire API只接受字典,因此请自行创建字典!
func toJSON() -> [String: Any] { return [ "name": name as Any,"age": age as Any,"marks": marks as Any ] }
Alamofire.request(URL,method:.put,parameters:example.toJSON(),encoding:JSONEncoding.default,headers :Defines.Api.Headers )
或者,使用SwiftyJSON:
func toJSON() -> JSON { return [ "name": name as Any,"marks": marks as Any ] }
用法:
Alamofire.request(URL,parameters:example.toJSON().dictionaryObject,headers :Defines.Api.Headers )