【问题标题】:SWIFT2: JSON request to array returns different valuesSWIFT2:对数组的 JSON 请求返回不同的值
【发布时间】: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


    【解决方案1】:

    实际上它非常简单:您的第二次打印将在 Alamofire 请求完成之前执行 - Alamofire.request 是稍后将执行的异步调用。

    您将在请求后获得您的信息,如果您需要设置一些 UI 元素,则必须调用某种重新加载视图或relaodData()(如果您使用的是UITableView)。

    您可以在此处调用reloadUI(),您的自定义函数:

    //PRINTS THE ARRAY
    reloadUI()
    print(cBlue)
    

    例子:

    类 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)
    
                    }
    
    
                case .Failure(_, let error):
    
                    print("Request failed with error: \(error)")
            }
            //PRINTS THE ARRAY
            reloadUI()
            print(cBlue) 
    
        }  
        //PRINT [] EMPTY ARRAY
        print(cBlue)     
      }
    }
    
    func reloadUI(){
        self.tagLabel.text = (cBlue[0] as! Info).tag
        self.locationLabel.text = (cBlue[0] as! Info).location
    }
    

    【讨论】:

    • 谢谢!因此,如果我想使用 Alamofire 并将结果打印到表中,我必须使用 reloadUI() 吗?
    • 是的,基本上,您必须通知视图或控制器或其他任何您实际收到数据并且 cBlue 数组已填充信息的东西。对于 tableView,您可以使用 reloadData() 方法来重新填充您的表。另一方面,如果您使用 UIView、UILabels 等,则必须创建自己的函数 - 答案是 reloadUI(),它将填充您的信息。
    • 嗯,我不明白。你能举个例子吗?
    • 如果您只使用 UIVIewcontroller,您可以调用 YourView.layoutSubviews()。而对于 UITabelviewcontroller 你应该调用 yourtableview.reloadData();
    • 谢谢。但是 reloadUI 不存在。我必须创建它吗?
    猜你喜欢
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2016-08-17
    • 2018-01-22
    • 2015-03-18
    相关资源
    最近更新 更多