swift2 - Append elements form JSON response to multidimensional array -
i using alamofire , swiftyjson.
code:
var array = array<array<string>>() alamofire.request(.get, url, parameters: ["posttype": "live"], encoding: parameterencoding.url).responsejson { (_, _, result) in switch result { case .success(let data): let json = json(data) for(_,subjson):(string, json) in json["info"] { let tag = subjson["tag"].string! let location = subjson["location"].string! array.append(array(count: 4, repeatedvalue: concerttag)) } case .failure(_, let error): print("request failed error: \(error)") } }
i want create array multidimensional. , add json variables after pass array tableview.
so need "array" like:
[[tag1; location1],[tag2, location2], .... ]
how can this? ideas?
thank you
you have replace
array.append(array(count: 4, repeatedvalue: concerttag))
with
array.append([tag, location])
and data example:
for arr in array { print("tag: " + arr[0]) print("loc: " + arr[1]) }
but suggest approach: using tuples.
first let's create type result because it's convenient:
typealias tagandlocation = (tag: string, location: string)
then prepare empty array of results:
var resulttuples = [tagandlocation]()
fill array of tuples in loop:
for (_, subjson) in json { let tag = subjson["tag"].string! let location = subjson["location"].string! let tagloc = (tag: tag, location: location) resulttuples.append(tagloc) }
then can access data in 2 ways:
for (tag, loc) in resulttuples { print("tag: " + tag) print("loc: " + loc) } result in resulttuples { print("tag: " + result.tag) print("loc: " + result.location) }
note: variables names should begin lowercase letter. beginning uppercase classes, types, protocols , such.
Comments
Post a Comment