SwiftyJSON ArrayValue数量有限的对象

时间:2017-04-13 07:05:38

标签: ios arrays json swift swifty-json

我在尝试获取内部有JSON数组的JSON时遇到了奇怪的问题。服务器API很好,因为Android应用程序和网站正在获取正确的数据,但在iOS上,我在这个数组中只获得50个或更少的对象。如果数组少于50个对象 - 我得到正确的数据。它可以以某种方式与SwityJSON限制相关联,还是其他一些不允许我获得整个JSON的东西?或者是100%服务器问题?

这是我的代码,但我相信,它应该没问题。

class func requestSelection(id: String, handler: @escaping (Selection?) -> Void) {
    let url = "selections/" + id

    APIModel.shared.requestWithLocation(.post, URL: url, parameters: nil) { data in

        guard let `data` = data else {
            handler(nil)
            return
        }

        let selection = data["selection"].to(type: Selection.self) as? Selection
        //I print "list" and its count is less or equals 50
        selection?.restaurants = data["selection"]["list"].to(type: Restaurant.self) as? [Restaurant]
        handler(selection)
    }
}

func requestWithLocation(_ httpmethod: Alamofire.HTTPMethod, URL: String, parameters: [String: AnyObject]?, completionHandler: @escaping CompletionHandler) {

    let parametersWithLocation = self.addLocationTo(parameters: parameters)
    let url = self.serverAddress + "/" + URL

    self.alamofireRequest(url: url, method: httpmethod, parameters: parametersWithLocation) { data in
        completionHandler(data)
    }
}

private func addLocationTo(parameters: [String: AnyObject]?) -> [String: AnyObject] {
    var location: [String: Double] = [:]
    let locationManager = CLLocationManager()

    if (CLLocationManager.authorizationStatus() == .authorizedWhenInUse || CLLocationManager.authorizationStatus() == .authorizedAlways) && locationManager.location != nil {
        location = [
            "lon": locationManager.location!.coordinate.longitude,
            "lat": locationManager.location!.coordinate.latitude,
        ]
    }
    var parametersWithLocation = parameters ?? [:]
    parametersWithLocation["location"] = location as AnyObject

    return parametersWithLocation
}

private func alamofireRequest(url: String, method: HTTPMethod, parameters: [String: AnyObject]?, completionHandler: @escaping CompletionHandler) {
    Alamofire.request(url, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: User.shared.header).validate().responseJSON { response in
        guard let data = response.result.value else {
            print(response.result.error as NSError? as Any, url)
            completionHandler(nil)
            return
        }
        //debugPrint(JSON(data), url)
        completionHandler(JSON(data))
    }
}

0 个答案:

没有答案