【问题标题】:Cannot pass dictionary with arrays to request body in swift无法通过数组传递字典以快速请求正文
【发布时间】:2017-02-01 06:48:30
【问题描述】:

我有一个名为 Beacon 的自定义类,它带有一个“toDictionary()”方法,该方法返回 Beacon 类的键值对。

let detectedBeacon = Beacon(id: "QA",
                                strength: Float(234),
                                proximity: "far",
                                time: self.getISODateFormat(),
                                wasOffline: false,
                                osVersion: "ios 10",
                                batteryLevel: "12",
                                uuid: "adadaad",
                                majorId: 4,
                                minorId: 5,
                                section: "QA",
                                region: "temp")


let array = [detectedBeacon.toDictionary()]

我用这个字典项创建了一个数组(目前数组中只有一个条目)。 现在我已将此数组包装到字典中,以便将其传递给 API 主体。

let locationParams : Dictionary<String, AnyObject> = ["locations" : array as AnyObject]
LocationApiManager.updateLastLocationOf(deviceId: deviceId, parameter: locationParams as [String : AnyObject]? ) {
        (response, errorMessage) in

    }

“updateLastLocation”方法调用alamofire post请求。

但是服务器接收到这个参数如下

    { 'locations[][section]': 'QA',
  'locations[][uuid]': 'adadaad',
  'locations[][region]': 'temp',
  'locations[][userId]': '0001',
  'locations[][minorId]': '5',
  'locations[][proximity]': 'far',
  'locations[][batteryLevel]': '12',
  'locations[][majorId]': '4',
  'locations[][beaconStrength]': '234',
  'locations[][time]': '2017-02-01 06:42:03 +0000',
  'locations[][osVersion]': 'ios 10',
  'locations[][beaconId]': 'QA',
  'locations[][wasOffline]': '0' }

为什么会这样?

更新

我的 toDictionary 方法

 func toDictionary() -> [String : AnyObject] {
    return (
        ["beaconId": self.beaconId as AnyObject,
         "beaconStrength": self.beaconStrength as AnyObject,
         "proximity": self.proximity as AnyObject,
         "time": NSDate() as AnyObject,
         "wasOffline": self.wasOffline as AnyObject,
         "osVersion": self.osVersion as AnyObject,
         "batteryLevel": self.batteryLevel as AnyObject,
         "uuid": self.uuid as AnyObject,
         "majorId": self.majorId as AnyObject,
         "minorId": self.minorId as AnyObject,
         "section": self.section as AnyObject,
         "region": self.region as AnyObject,
         "userId": "0001" as AnyObject]
    )
}

更新位置方法包含一些类似这样的代码:

class func postData(apiEndPoint: String,
                     parameters: [String: AnyObject]?,
                        headers: HTTPHeaders,
                     completion:
                        @escaping (_ responseFromBaseAPI : [String : AnyObject]?) ->()) -> Bool {
    UIApplication.shared.isNetworkActivityIndicatorVisible = true


    Alamofire.request(apiEndPoint,
                            method: .post,
                        parameters: parameters,
                          encoding: URLEncoding.httpBody,
                           headers: headers).responseJSON { result in
            completion(result.result.value as? [String: AnyObject])
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
        }
    return true
}

【问题讨论】:

    标签: swift rest nsdictionary alamofire


    【解决方案1】:

    好吧,您在这里创建了一个数组,其中包含字典作为唯一元素:

    let array = [detectedBeacon.toDictionary()]
    

    你不想在location键下直接传字典吗? 您可以尝试只传递detectedBeacon.toDictionary() 而不是[detectedBeacon.toDictionary()]

    实际上我的意思是LocationApiManager.updateLastLocationOf 可能期望不同的结构。这个方法是如何实现的?它期望什么参数?

    更新:

    那么,您对服务器的实际期望是什么?你不是在发送 JSON,而只是一个字典的字符串表示,对吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2011-09-19
      • 2012-07-12
      • 2020-10-22
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多