简体   繁体   English

Xcode跳过JSONObjectWithData

[英]JSONObjectWithData is being skipped by Xcode

The following code is supposed to load "groups" into a table view, however, the table view is blank: 以下代码应该将“组”加载到表视图中,但是该表视图为空白:

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!")
}

I tried setting a breakpoint at if let jsonDataArray... and the Xcode stopped at the line. 我试图在if let jsonDataArray...处设置断点,并且Xcode停在该行。 However, when I stepped forward, it directly skipped to print("lol") . 但是,当我上前时,它直接跳到print("lol") I am confused why this is happening. 我很困惑为什么会这样。 Is it because jsonDataArray is returning nil? 是因为jsonDataArray返回nil吗?


EDIT: Here is the full function 编辑:这是全部功能

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")
        }
    })
}

You are setting a breakpoint at the call to a function which executes a closure and so if you just try to step through from there it will appear as if the inside is not being executed when in fact, it is. 您是在对执行闭包的函数的调用处设置断点,因此,如果您仅尝试从那里逐步执行,则会好像实际上没有在执行内部一样。 Set a breakpoint at a line inside the execution of the closure, such as at the id line, where you'll then be able to see the status of all your data, if there is any. 在闭包执行过程中的一行上设置一个断点,例如id行,然后您可以在其中查看所有数据的状态(如果有的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM