【发布时间】: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