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 -

Python Tornado package error when running server -

Qt QGraphicsScene is not accessable from QGraphicsView (on Qt 5.6.1) -