-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Utilizing r.json response? #127
Comments
I believe you have to cast it first. let result = r.json! as! [String: String]
print(result["my_key"]) I am new as well, so I'm not sure if there is a better approach, but Python and Swift are very different languages. Python is interpreted language and Swift is a compiled language. So you have to cast it first. This is an example when using Reddit api to login:
struct LoginModel: Decodable {
let access_token: String
let expires_in: Int
let scope: String
let token_type: String
} And after getting the response from if r.ok {
let resp = try? JSONDecoder().decode(LoginModel.self, from: r.content!)
} Then you can get the values: print(resp.access_token) |
Thank you for this! I'll give it a try. |
Apologies if this is a dumb question, but I am relatively new to Swift. I've implemented this in a little sandbox and can properly make a
post
call to my end point and get a proper response.I'm very familiar with Python requests, so I was hoping that I would be able to address the r.json object similarly in Swift, but no matter what I do, I can't seem to figure out how to actually get key out of the r.json object. I can print it and see it is valid, but when I try to something simple (in my mind) like
print(r.json["my_key"])
I can't.This is probably more of a lack of understanding of the object type that r.json returns in swift and how to get info out of it, but I figured I'd ask because I love the idea of just using my familiarity with Python requests to start my voyage into Swift programming.
The text was updated successfully, but these errors were encountered: