swift - 使用 Alamofire 和 SwiftyJSON 发布对象数组

标签 swift alamofire swift4 swifty-json

我有一系列字典,我想借助 SwiftyJSON 将它们发布到 Alamofire。

api 设置为:

[
    {
        "imageUrl": "someimage3.jpg"
    },
    {
        "imageUrl": "someimage4.jpg"
    }
]

我的包含图像对象的数组在打印时看起来像这样,其中包含 imageUrl 键和图像名称作为值。

uploadedFiles = [
    [imageUrl: "someimage.jpg"],
    [imageUrl: "someimage2.jpg"]
]

我正在尝试将字典数组转换为正文所需的格式。我不太确定如何让它们成为 [String: AnyObject]

var body: [String: AnyObject] = [:]
let paramsJSON = JSON(uploadedFiles)
body = paramsJSON

alamofire 帖子

Alamofire.request("\(BASE_URL)mainimages/\(someid)", method: .post, parameters: body, encoding: JSONEncoding.default, headers: header).responseString { (response) in
        if response.result.error == nil {
            let status = response.response?.statusCode
            completion(true, status)
        } else {
            completion(false, nil)
            debugPrint(response.result.error as Any)
        }
    }

最佳答案

你可以用这个方法实现,它对我有用。

let payload = [["eventName": "Notifications","body":["version": "1.0","latitude": lat,"longitude":lon]]] as [[String : AnyObject]]

trackEventRequest(requestParams: payload, urlString: "https://xyz/youyURL")





func trackEventRequest(requestParams: [[String: AnyObject]], urlString: String) {

        let url = URL(string: urlString)
        var request = URLRequest(url: url!)
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpMethod = "POST"
        request.httpBody = try! JSONSerialization.data(withJSONObject: requestParams, options: [])

        Alamofire.request(reqeust).responseJSON { (response) in
            switch response.result {
            case .success:
                print(response.result.value)
                break
            case .failure:
                print(response.error)
                break
            }
        }
    }

关于swift - 使用 Alamofire 和 SwiftyJSON 发布对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50035782/

相关文章:

ios - 如何在编辑表格 View 单元格中的文本字段时重新加载所有表格 View 单元格 - iOS Swift

swift - 如何正确处理多个 if else 条件?

swift - 访问 block 中的值时出现 EXC_BAD_ACCESS

ios - Alamofire 请求给出内存警告

ios - 模块 'Alamofire' 是为不兼容的目标 arm64 - apple- ios 10 创建的

ios - 用 Swift 4 编写的 Pod

ios - 如何使用 CoreLocation 在您当前的位置添加注释

ios - 创建像 iOS 日历一样的 TableView

swift - Codable/Decodable 应该用字符串解码数组

swift - 使用 multipartFormData 上传图像时 Alamofire 请求正文为零