无法转换 'String?' 类型的值 (Alamofire)

Cannot convert value of type 'String?' (Alamofire)

我用对象映射器映射我的 class 用户:

class User: Mappable{

private var _username: String! = nil
private var _password: String! = nil
private var _firstName: String! = nil
private var _lastName: String! = nil

//here are getters and setters(it takes too much space)

init(){

}

required init?(_ map: Map) {

}

func mapping(map: Map) {

    username <- map["USERNAME"]
    password <- map["PASSWORD"]
    firstName <- map["FIRST_NAME"]
    lastName <- map["LAST_NAME"]

  }
}

然后我尝试创建新用户并输入一些值,映射整个对象并像这样使用 Alamofire 发送:

let userEmail = userEmailField.text!
let userPassword = userPasswordField.text!

let user = User()
user.username = userEmail
user.password = userPassword
let JSONString = Mapper().toJSONString(user, prettyPrint: true)

AlamofireService.alamofireService.makePostServiceRequest(URL_BASE, parameters: JSONString, resposeCallback: self)

我按照库的说明进行操作,但出现错误 "Cannot convert value of type 'String?' to expected argument type '[String : AnyObject]'",为什么?

您不需要将参数转换为字符串,只需将参数作为字典传递给 Alamofire。

Alamofire 请求采用 [String: AnyObject] 格式的参数,因此您只需要将字典作为参数传递即可。在您的情况下,您只需要:

let userEmail = userEmailField.text!
let userPassword = userPasswordField.text!

var dictParameter: Dictionary<String, AnyObject> = [:]
dictParameter["USERNAME"]   = userEmail
dictParameter["PASSWORD"]   = userPassword

之后只需提出请求:

AlamofireService.alamofireService.makePostServiceRequest(URL_BASE, parameters: dictParameter, resposeCallback: self)