Search code examples
jsonios9alamofireswift2

Alamofire json swift 2.2


I'm trying to take out some data of facebook place's names with Alamofire but i have no luck. This is what i've tried and i cant figure out how to make it work.

xcode 7.3 swift2.2

So the result of the graph api is this

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

and i want to go inside "data" and take only the name and the ID of the place which are the 2 last lines.

And this is my code!

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

and i get this error

fatal error: unexpectedly found nil while unwrapping an Optional value

Any idea why?


Solution

  • Ok this was easy i guess and it didn't worth the question but this is the solution

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

    And at the cell

    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
    }
    

    Just dont forget to use SwiftyJSON too!