Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

error converting dictionary values #54

Closed
JohnCoates opened this issue May 13, 2016 · 2 comments
Closed

error converting dictionary values #54

JohnCoates opened this issue May 13, 2016 · 2 comments

Comments

@JohnCoates
Copy link

JohnCoates commented May 13, 2016

Great library!

I tried to convert a dictionary and got this error:

UpdateResponse error: TypeMismatchError("users", [greg: {
    name = greg;
}], Swift.Dictionary<Swift.String, Swift.Dictionary<Swift.String, FlexApp.User>>)

Here's some sample code to reproduce the error:

struct User: Mappable {
    let name:String

    init(map: Mapper) throws {
        try name = map.from("name")
    }
}

struct UserList: Mappable {
    let users:[String:[String:User]]

    init(map: Mapper) throws {
        do {
            try users = map.from("users")
        }
        catch let error {
            print("UpdateResponse error: \(error)")
            throw error
        }
    }
}

 let JSON = [
            "users": [
                "greg": [ "name" : "greg"]
            ]
        ]
let userList = UserList.from(JSON)
print("user list: ", userList)
let user = User.from(JSON["users"]!["greg"]!)
print("user: ", user)
@keith
Copy link
Contributor

keith commented May 13, 2016

Thanks!

So the problem here is something that will be solved by some variation of #7

The problem here is that at the base level we're just doing foo as? [String: [String: User]] which doesn't work, but it still compiles. What you'll want to do here is probably a [String: NSDictionary], and then map over that to get the second dictionary.

@keith
Copy link
Contributor

keith commented Jul 13, 2016

Closing this, feel free to reopen this if there's still an issue here. I've merged #59 so you should get a compile time error for anything that would fail at runtime as soon as I create a new release. If you find that isn't the case please open an issue!

@keith keith closed this as completed Jul 13, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants