2016-04-22 16 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

は、[OK]を、これは簡単だった私は推測する、それが問題価値はなかったが、これは解決策

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を使用することを忘れていけません!

関連する問題