以下代码应该将“组”加载到表视图中,但是该表视图为空白:

do {
    if let jsonDataArray = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) as? NSArray {
        for imageDataDict in jsonDataArray {
            let group = Group(
                id: imageDataDict.valueForKey("id") as! Int,
                name: imageDataDict.valueForKey("name") as! String,
                author: imageDataDict.valueForKey("author") as! Int
            )
            self.dataArray.append(group)
        }
        self.tableView.reloadData()
    }
    print("lol")
} catch {
    // report error
    displayAlertMessage("Error", alertDescription: "Oh man, there was an error!")
}


我尝试在if let jsonDataArray...处设置断点,并且Xcode在该行处停止。但是,当我上前时,它直接跳到了print("lol")。我很困惑为什么会这样。是因为jsonDataArray返回nil吗?



编辑:这是全部功能

func loadSelfieData () {
    // Create HTTP request and set request Body
    let defaults = NSUserDefaults.standardUserDefaults()
    let user_id = defaults.integerForKey("user_id")
    let httpRequest = httpHelper.buildRequest("groups?user_id=\(user_id)", method: "GET",
        authType: HTTPRequestAuthType.HTTPTokenAuth)

    httpHelper.sendRequest(httpRequest, completion: {(data:NSData!, error:NSError!, description:NSMutableString!) in
        if error != nil {
            let errorMessage = self.httpHelper.getErrorMessage(error)
            displayAlertMessage("Error", alertDescription: errorMessage as String)

            return
        }

        var _ :NSError?

        do {
            if let jsonDataArray = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) as? NSArray {
                for imageDataDict in jsonDataArray {
                    let group = Group(
                        id: imageDataDict.valueForKey("id") as! Int,
                        name: imageDataDict.valueForKey("name") as! String,
                        author: imageDataDict.valueForKey("author") as! Int
                    )
                    self.dataArray.append(group)
                }
                self.tableView.reloadData()
            }
            print("lol")
        } catch {
            displayAlertMessage("Error", alertDescription: "there was an error")
        }
    })
}

最佳答案

您是在对执行闭包的函数的调用处设置断点,因此,如果您仅尝试从那里逐步执行,则实际上似乎未在执行内部操作。在闭包执行过程中的一行上(例如id行)设置一个断点,然后您就可以在其中查看所有数据的状态(如果有)。

08-07 19:26