无法使用数组传递字典以在swift中请求body

时间:2017-02-01 06:48:31

标签: swift rest nsdictionary alamofire

我有一个名为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

    }

&#34; updateLastLocation&#34;方法调用alamofire post request。

但服务器接收此参数如下

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

1 个答案:

答案 0 :(得分:0)

好吧,你在这里创建一个包含字典的数组:

let array = [detectedBeacon.toDictionary()]

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

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

<强>更新

那么,您对服务器的期望是什么?您没有发送JSON,只是发送字典的字符串表示吗?