0
    _ = URLSession.shared.dataTask( with: request ) {
        data, response, error in
        // ...
    }

Assuming this code produces a successful load, data could be some JSON, or an image, or something else. What is [a way / the best way] to differentiate between different data types on response?

Or, perhaps, is it best to have multiples outlets for URLSession in one's app that each handle a different Data type?

Thank you for reading.


This question is not a duplicate of Determine MIME type from NSData? because in order to discover that post, the searcher would be required to: 1. know what a MIME type is; and 2. know what they are for, and therefore use the keyword "MIME" in their search. Although the question posed by this post is answered by the "Determine MIME..." post, its relevance is in providing a searching bridge for people to whom the use of MIME types isn't immediately obvious.

Joseph Beuys' Mum
  • 2,395
  • 2
  • 24
  • 50
  • A proper implemented server should return the data type as well. Inspect the HTTP header field `Content-Type` which should contain the MIME type of the response. – clemens Feb 11 '18 at 11:00
  • Thanks @clemens. What is the "server" is just an image on a http url? And what is the server is someone else's server that I can't access that hasn't been properly implemented, how do I get the MIME type then? – Joseph Beuys' Mum Feb 11 '18 at 11:04
  • 1
    You should check the responses with `curl`. With the option `-v` you can see all HTTP headers. – clemens Feb 11 '18 at 11:06
  • thanks again @clemens. and thank you to whoever upvoted the downvote this question at first received – Joseph Beuys' Mum Feb 11 '18 at 13:13
  • Question for @dávid-pásztor: if you didn't know about the importance of MIME types in answering this question, how would you be able to search to find the post entitled "Determine MIME type from NSData?" on the site? This is the question that, for me, shows that my question is *not* a duplicate of "Determine MIME type from NSData?" – Joseph Beuys' Mum Feb 11 '18 at 20:27
  • the above comment also applies to @rmaddy – Joseph Beuys' Mum Feb 11 '18 at 20:27
  • and to @clemens – Joseph Beuys' Mum Feb 11 '18 at 20:28
  • To assume that people have heard of MIME types and/or know what they are for breaks the principle of inclusivity. And it's 2018 everyone, inclusivity should come as standard. – Joseph Beuys' Mum Feb 11 '18 at 20:31
  • ok @haroldo-gondim, sure, let's just let people who don't know that NS/Data objects contain useful MIME type information flounder in the darkness. I'm sure that's better – Joseph Beuys' Mum Feb 12 '18 at 11:03

0 Answers0