2016-04-22 83 views
2

我试图拿出一些与Alamofire的facebook地名的数据,但我没有运气。 这是我试过,我不知道如何使它工作。Alamofire json swift 2.2

的Xcode 7.3 swift2.2

使图形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" 
     } 

,我想进去 “数据”,并采取只有名称和最后2行的地方的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"]!)") 
     } 
} 
} 

和我得到这个错误

fatal error: unexpectedly found nil while unwrapping an Optional value

任何想法,为什么?

+0

print(“JSON:\(JSON)”)将是一个好的开始。你强制解开“数据”和“名称”。如果说你的JSON与你期望的不同,那么任何一次都会失败。在任何情况下,不建议在这种情况下强制拆包,更好地使用“如果让”。可能是某种“错误-JSON”。 – bauerMusic

回答

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呢!