2

我正在尝试用 Alamofire 取出一些 Facebook 地名的数据,但我没有运气。这是我尝试过的,我不知道如何使它工作。

xcode 7.3 swift2.2

所以graph api的结果是这样的

{
   "data": [
      {
         "category": "Local business",
         "category_list": [
            {
               "id": "272705352802676",
               "name": "Outdoors"
            },
            {
               "id": "115725465228008",
               "name": "Region"
            }
         ],
         "location": {
            "street": "Athens",
            "city": "Palai\u00f3n F\u00e1liron",
            "state": "",
            "country": "Greece",
            "zip": "17562",
            "latitude": 37.9284637008,
            "longitude": 23.6944070162
         },
         "name": "THE NAME OF THE PLACE",
         "id": "THE ID"
      }

我想进入“数据”,只取最后两行的地方的名称和 ID。

这是我的代码!

override func viewDidLoad() {
    super.viewDidLoad()

 Alamofire.request(.GET, "https://graph.facebook.com/search", parameters: ["q": "", "type": "place", "center": "37.928319,23.7036673", "distance": "10000", "access_token": "ACCESS-TOKEN", "expires_in": "5184000"])
    .responseJSON { response in
        if let JSON = response.result.value {
            print("JSON: \(JSON["data"]!["name"]!)")
        }
}
}

我得到这个错误

致命错误:在展开可选值时意外发现 nil

知道为什么吗?

4

1 回答 1

0

好的,我猜这很容易,这不值得这个问题,但这是解决方案

 Alamofire.request(.GET, "https://graph.facebook.com/search", parameters: ["q": "", "type": "place", "center": "37.928319,23.7036673", "distance": "10000", "access_token": "475827182628380|6U-mk-1etGQHwrXRSMF4Ht2zOyc", "expires_in": "5184000"])
            .responseJSON { (responseData) -> Void in
                if((responseData.result.value) != nil) {
                    let swiftyJsonVar = JSON(responseData.result.value!)

                    if let resData = swiftyJsonVar["data"].arrayObject {
                        self.arrRes = resData as! [[String:AnyObject]]
                    }
                    if self.arrRes.count > 0 {
                        self.kati.reloadData()
                    }
                }
        }

在牢房里

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("checkInCell", forIndexPath: indexPath)
    var dict = arrRes[indexPath.row]
    cell.textLabel?.text = dict["name"] as? String
    //cell.detailTextLabel?.text = dict["email"] as? String
    return cell
}

只是不要忘记使用 SwiftyJSON!

于 2016-04-22T19:38:43.287 回答