Cole
Cole

Reputation: 2646

Object always nil while using ObjectMapper and Alamofire

It seems that no matter what I do, my converted JSON variable in Swift is always nil. I'm following the documentation for AlamofireObjectMapper to the letter and nothing seems to be working.

func loginAF(username: String, password: String, url: String) {

        Alamofire.request(.GET, url, parameters: ["username": username, "passoword": password]).responseObject { (response: Resultset?, error: NSError?) in
            println(response!)
            println(response?.account?.mongoId)
        }
    }

That last print statement is nil

Here is the Resultset struct, which is what it should be pulling from:

struct Resultset: Mappable {
    var account: Account?

    init(){}
    init?(_ map: Map) {
        mapping(map)
    }

    mutating func mapping(map: Map) {
        account <- map["account"]
    }
}

struct Account: Mappable {
    var mongoId: Int?

    init() {}
    init?(_ map: Map) {
        mapping(map)
    }

    mutating func mapping(map: Map) {
        mongoId <- map["mongoId"]
    }
}

I know for sure I'm getting valid data, here's a taste of what it looks like:

{"errors":[],"resultset":{"account":{"mongoId":"55a7961fdf5d3ca421ff4cb9"},"locations":[{"......

If anyone knows any other way to convert JSON to objects easily, please let me know. Thanks!

Upvotes: 0

Views: 1357

Answers (1)

TPlet
TPlet

Reputation: 948

one of your parameters in the request is called passoword, and secondly which i think is the solution is that you cannot use structs.

This is due to the fact that Serializer’s are expected to return AnyObject as its result, and structs are not AnyObject, which is a shame since structs are the perfect value types.

I got this from here
http://kaandedeoglu.com/2015/02/20/From%20JSON%20to%20Type-safe%20objects%20in%20Swift/

Additionally in the Documentation I just saw classes.
I hope this helps you

Upvotes: 1

Related Questions