代码之家  ›  专栏  ›  技术社区  ›  Konstantinos Natsios

Alamofire json swift 2.2版

  •  2
  • Konstantinos Natsios  · 技术社区  · 9 年前

    我正在尝试用Alamofire获取一些facebook地名的数据,但我运气不好。 这是我尝试过的,但我想不出如何让它发挥作用。

    xcode 7.3 迅捷2.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"
          }
    

    我想进入“data”,只取最后两行的名字和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

    知道为什么吗?

    1 回复  |  直到 9 年前
        1
  •  0
  •   Konstantinos Natsios    9 年前

    好吧,我想这很简单,不值得提问,但这是解决办法

     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!