aronbalog / Vox

Swift JSON:API client framework
http://jsonapi.org
MIT License
46 stars 19 forks source link

Creating Resource - Issue unwrapping response #24

Closed ehubbell closed 5 years ago

ehubbell commented 5 years ago

Hi There -

I've got a JSONAPI compliant Rails server that I'm using with VOX on a pretty simple Swift app. When tying to create a resource as per the README, I'm running into issues unwrapping the server response which it appears VOX is wrapping in an optional. I've currently got the following:

func authenticate(auth: AuthResource, completion: @escaping (AuthResource) -> ()) {
    let auth = AuthResource()
    auth.email = emailField.text
    auth.password = passwordField.text

    try? dataSource.create(auth).result({ (document: Document<AuthResource>?) in
         let auth = document?.data
         completion(auth)
     }) { (error) in
         print(error!)
     }
}

When I inspect the auth variable I see the server response && document tree however I'm having trouble unwrapping and then gaining access to the variables from the server response. A simple p auth returns nil and if I wrap the completion statement it in an if let it doesn't do anything.

I'm pretty new to Swift but hoping I can get this to work. Any help is appreciated!

ehubbell commented 5 years ago

Closing this issue. I was sending in an AuthResource and then returning a UserResource in the server response. Didn't realize that keep the document?.data method from unwrapping.