以下代码应该将“groups”加载到表视图中,但是,表视图是空白的:
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")
}
})
}
答案 0 :(得分:0)
你正在调用一个执行闭包的函数时设置一个断点,所以如果你只是尝试从那里逐步执行,它就会显得好像内部没有被执行,实际上它是。在闭包执行中的一行内设置一个断点,例如在id行,然后你可以看到所有数据的状态(如果有的话)。