ios - Handle JSON response data and String response data respectively in callback -


my backend can either return json or plain string data in response.

let task = session.datatask(with: request urlrequest) { data, response, error in     if error == nil {        // handle response data, json & string data, how check , distinguish & handle respectively?        if let data = data {                 // fails here if data string                 if let jsondatadict = try? jsonserialization.jsonobject(with: data) as? jsondatadict {                  } else {           }      } } 

in response callback, how check wither response data json format or string , how handle respectively?

try string initialiser passing data utf8 string encoding

if let data = data {                 // fails here if data string                 if let jsondatadict = try? jsonserialization.jsonobject(with: data) as? jsondatadict {                  } else {                 if let datastring = string(data: data, encoding: .utf8){                       debugprint(datastring)                 }           }      } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -