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