Bojan Radulovic Bojan Radulovic - 4 months ago 31
Swift Question

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

I map my class User with object mapper:

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"]

}
}


And then i tried to make new user and put some values, map the whole object and send with Alamofire like this:

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)


I followed the instructions of libraries, but i get the error "Cannot convert value of type 'String?' to expected argument type '[String : AnyObject]'", why?

Answer

Alamofire request taking parameter in [String: AnyObject] format so you just need to pass dictionary as parameter. in your case you just need like:

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

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

And after that Just make request:

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