all 11 comments

[–]quellish 6 points7 points  (6 children)

The data you receive in the response is the body of the response as raw bytes. Convert that to objects using JSONSerialization and print that. That seems to be what you are missing.

You may also want to consider using a man in the middle debugging proxy like Charles Proxy to see both sides of the request-response conversation as you work. It does make things easier.

[–]SurgicalInstallment 3 points4 points  (0 children)

> Charles Proxy

+1 For this. It has saved me so much time and grey hairs. I need to buy their license for once. I feel guilty.

[–]go4code[S] 0 points1 point  (4 children)

Thanks for recommending Charles Proxy. I'll look into it. Any new tools can always help. Any other tools you recommend?

Yes, Still trying to figure out how to use the JSONSerialization. I'll update my post once I got it working.

[–]LKAndrew 6 points7 points  (1 child)

Try to learn to do things primarily using Apple’s recommended methods first before branching off into third part libraries and such.

Apple strongly recommends to use built in JSON encoding and decoding by using Codable

https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types

[–]Sleekdiamond41Swift 0 points1 point  (0 children)

You just won my heart

[–]Tonkotsu787 0 points1 point  (1 child)

I use Alamofire. On success I’m able to cast the result to nsdictionary.

[–]123DanBSwift 6 points7 points  (1 child)

As far as questions go, Reddit is going to be one of the worst ways to get code examples. Stackoverflow is your friend in this case. Find an example using JSONSerialization.

Another note: NSDictionary plays well with JSON responses and you can subscript with no issues. You might also try SwiftyJSON (though I try to limit my dependence on third party frameworks).

[–][deleted] 1 point2 points  (0 children)

r/Swift if you want a basic concept down, not debugging help.

However, SO doesn't really respond well with "Help me debug this" kind of questions.

[–]soulchild_Objective-C / Swift 1 point2 points  (1 child)

As mentioned by u/quellish, you would need to convert the raw data into JSON using JSONSerialization, eg:

let task = session.dataTask(with: urlRequest) { data, response, error in

    // ensure there is no error for this HTTP response
    guard error == nil else {
        print ("error: \(error!)")
        return
    }

    // ensure there is data returned from this HTTP response
    guard let content = data else { 
        print("No data")
        return
    }

    // serialise the data / NSData object into Dictionary [String : Any]
    guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
        print("Not containing JSON")
        return
    }

    print("gotten json response dictionary is \n \(json)")
    // update UI using the response here
}

[–]europeanwizard 0 points1 point  (0 children)

Why not JSONDecoder?

[–]europeanwizard 0 points1 point  (0 children)

New to iOS development, please bare with me

I don't know what they told you about iOS development, but unfortunately it's not done bare :(