ios - Storing fetched json dictionaries in array resulting in nil -
i have database of files attributes such url, title, , timestamp. when load view controller, i'm fetching file data , loading tableview. because there multiple files each 3 attributes, i'm trying save data in array, go through each object in array , extract json data. of now, request , response successful, array remains nil. approach wrong?
let task = session.datataskwithrequest(request, completionhandler: {data, response, error -> void in guard let data = data, response = response else { print("no data or response!") return } let strdata = nsstring(data: data, encoding: nsutf8stringencoding) print("body: \(strdata)", terminator: "") { self.fetchedarray = try nsjsonserialization.jsonobjectwithdata(data, options: .mutableleaves) as? nsarray print("fetched array count %i \(self.fetchedarray!.count)") if let jsonarray: nsarray = self.fetchedarray{ var = 0; i<jsonarray.count; ++i { let dictresult = jsonarray.objectatindex(i) as! nsdictionary let recording = recordings() recording.trackurl = dictresult["url"] as? string recording.tracktimestamp = dictresult["timestamp"] as? string recording.trackauthor = dictresult["author"] as? string recording.tracktitle = dictresult["title"] as? string self.recordings?.addobject(recording) } } else { // no error thrown, not nsdictionary let jsonstr = nsstring(data: data, encoding: nsutf8stringencoding) print("no error thrown not parse json: '\(jsonstr)'") } } catch let parseerror { // log error thrown `jsonobjectwithdata` print(parseerror) let jsonstr = nsstring(data: data, encoding: nsutf8stringencoding) print("error thrown. not parse json: '\(jsonstr)'") } }) task.resume()
as executing second last else statement print("no error thrown not parse json: '\(jsonstr)'")
, printing out data.
log of data before serialization:
response: <nshttpurlresponse: 0x7c349990> { url: http://127.0.0.1:5000/auth/serve_current_user_files } { status code: 200, headers { "content-length" = 239; "content-type" = "application/json"; date = "sat, 03 oct 2015 02:53:00 gmt"; server = "werkzeug/0.10.4 python/2.7.10"; "set-cookie" = "session=eyjfznjlc2gionrydwusil9pzci6eyigyii6ik16rxhargs0tldvm1luqmxnrejrwlrnefpqz3pzmkl3t1drne5ezzvprek9in0sinvzzxjfawqioiixin0.cpduja.mm56vpuzpiokczvorw7x2ysz960; httponly; path=/"; } }body: optional({ "recordings": [ { "author": 1, "timestamp": "sun, 27 sep 2015 17:44:54 gmt", "title": "test1.m4a", "url": "/users/allahesharghi/desktop/omid/pythonprojects/sound/sound/uploads/omid1/test1.m4a" } ]
your json is:
{ "recordings": [ { "author": 1, "timestamp": "sun, 27 sep 2015 17:44:54 gmt", "title": "test1.m4a", "url": "/users/allahesharghi/desktop/omid/pythonprojects/sound/sound/uploads/omid1/test1.m4a" } ] }
so, root object isn't array, dictionary array of dictionaries key recordings
. means when say
self.fetchedarray = try nsjsonserialization.jsonobjectwithdata(data, options: .mutableleaves) as? nsarray
self.fetchedarray nil because downcast nsarray
fail, because nsdictionary can't downcast nsarray.
you need access root dictionary , access array recordings key -
do { somedictionary = try nsjsonserialization.jsonobjectwithdata(data, options: .mutableleaves) as? nsdictionary self.fetchedarray = somedictionary!["results"] as? nsarray print("fetched array count %i \(self.fetchedarray!.count)") ...
Comments
Post a Comment