小编Rya*_*ton的帖子

如何对来自Alamofire的JSON进行排序并返回最终的JSON对象(swiftyJSON)

我无法简洁地从api中提取数据,将用户当前位置添加到对象中,然后根据计算的距离对数据进行排序.

stackoverflow问题并没有完全解决我面临的问题.请参阅:如何对从Swift中的JSON服务器文件中读取的帖子进行排序.

我目前正在从Alamofire加载api数据并使用UITableViewController渲染该数据.

    override func viewDidLoad() {
    super.viewDidLoad()
    titleLabel.title = q.capitalizedString
    Alamofire.request(.GET, "http://www.thesite.com/api/v1/things", parameters: ["q" : q])
        .responseJSON { response in
            let JSONObject = JSON(response.result.value!)
            self.results = JSONObject["things"]
            for (key, _) in self.results {
                let intKey: Int = Int(key)!
                var thisItem: JSON = self.results[intKey]
                let geoLat = thisItem["place"][0]["location"]["geo"][1].double ?? 37.763299
                let geoLong = thisItem["place"][0]["location"]["geo"][0].double ?? -122.419356
                let destination = CLLocation(latitude: geoLat, longitude: geoLong)
                let setLocation = CLLocation(latitude: self.currentLocation.latitude, longitude: self.currentLocation.longitude)
                let distanceBetween: CLLocationDistance = destination.distanceFromLocation(setLocation)
                thisItem["distance"].double =  distanceBetween …
Run Code Online (Sandbox Code Playgroud)

swift alamofire swifty-json

2
推荐指数
1
解决办法
5288
查看次数

标签 统计

alamofire ×1

swift ×1

swifty-json ×1