ios - Unable to iterate through array -
ios - Unable to iterate through array -
i'm trying info local server while parsing json. in view controller access local ruby on rails server:
override func viewdidload() { super.viewdidload() var url = nsurl(scheme: "http", host: "0.0.0.0:3000", path: "/api/search?query=auto") var request = nsurlrequest(url: url) allow task = nsurlsession.sharedsession().datataskwithrequest(request) {(data, response, error) in var data1: nsdictionary = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsdictionary println(data1) println(data1["companies"]) var companies = data1["companies"] nsarray company: anyobject in companies { println(company) } } task.resume() }
println(data1)
returns:
{ companies = "[{\"alias\":\"02-avtomoyka-tulybaev-r-t\",\"id\":45693},{\"alias\":\"1001-zapchast-internetmagazin-avtotovarov\",\"id\":42621}]"; }
with println(data1["companies"])
, i've got array of objects in console:
[{"alias":"02-avtomoyka-tulybaev-r-t","id":45693},{"alias":"1001-zapchast-internetmagazin-avtotovarov","id":42621}]
then, when i'm trying iterate through array, app crashes. doing wrong? stucks no errors in console, (lldb)
, exc_breakpoint(code=exc_i386_bpt, subcode 0x0)
in tread 6
ps: xcode throws compiler issue constant 'company' inferred have type 'anyobject', may unexpected
@ for company in companies {
string. how can handle it?
instead of var companies = data1["companies"] nsarray
seek var companies = data1["companies"].allvalues;
the problem data1["companies"]
dictionary , can't implicitly converted nsarray
. compiler doesn't grab because data1["companies"]
stored in nsdictionary
anyobject
, allow cast subclass.
ios swift
Comments
Post a Comment