【发布时间】:2015-11-13 23:16:50
【问题描述】:
我正在使用 Alamofire 和 SwiftyJSON 将 JSON 数据传递给数组。
第一次打印返回一个正确的数组:
[(1, "Arena", "Oklahoma"), (2, "Stafium", "Berlin")]
但第二个打印一个空数组:
[]
我不明白为什么?
这是我的代码。 由@NickCatib 解决
typealias cType = (ID: Int,Tag: String, Location: String)
var cBlue = [cType]()
var NumRows = 0
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, url, parameters: ["postType": "live"]).responseJSON { (_, _, result) in
switch result {
case .Success(let data):
let json = JSON(data)
for(_,subJSON) in json["LocalInfo"] {
let ID = subJSON["id"].int!
let Tag = subJSON["Tag"].string!
let Location = subJSON["Location"].string!
let Info = (ID: ID, Tag: Tag, Location: Location)
cBlue.append(Info)
}
self.tableView.reloadData()
case .Failure(_, let error):
print("Request failed with error: \(error)")
}
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
reloadUI()
return cBlues.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("CellConcert",
forIndexPath: indexPath)
let info = cBlue[indexPath.row] as! Info
cell.textLabel?.text = info.Tag
return cell
}
}
这是正确的吗?
谢谢
【问题讨论】:
标签: arrays json tuples swift2 alamofire