ios - 发送到 Swift 中的 firebase 云函数后,字典对象数组会发生变化吗?

标签 ios swift firebase google-cloud-functions

我有一个名为 assets 的字典对象数组,当在 Swift 中打印时,它显示:

[
    ["assetType": "video", "assetPath": "some_path"],
    ["assetType": "image", "assetPath": "some_other_path"]
]

这很完美,但是,当我将其发送到我的 firebase 云函数并在云函数中打印出来后,assets 变为:

[
    {
        "assetType": ["video", "image"],
        "assetPath": ["some_path", "some_other_path"]
    }
]

为什么会发生这种情况?我该如何解决这个问题?

----------------------------更新-------------- -----------------

我使用Alamofire模块来执行http请求:

Alamofire.request(
    "https://....",
    method: .post,
    parameters: [
        "assets": assets
    ]
)

--------------------------------------------更新 2------------ ------------------

我的云功能如下:

exports.testFunction = functions.https.onRequest((req, res) => {
    const { assets } = req.body;
    return res.status(200).send(assets)
})

一旦被调用就会立即发回 Assets

最佳答案

更新:Alamofire 执行此操作的方法位于底部

我尝试了其他方法,但只能回答问题的第二部分:我该如何解决这个问题?。显然,不使用 Alamofire 可以解决问题,但我确信 Alamofire 没有任何问题,可能缺少一些东西,我会进一步挖掘,但现在, Alamofire 的替代方案是 URLSession:

let session = URLSession.shared
let url: URL = URL(string: "https://...")!
let request = NSMutableURLRequest(url: URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

do {
    request.httpBody = try JSONSerialization.data(
        withJSONObject: [
            "assets": assets
        ],
        options: JSONSerialization.WritingOptions()
    )
    let task = session.dataTask(with: request as URLRequest) { (data, _, _) in
        if let responseData = data {
            print(String(data: responseData, encoding: .utf8)!)
        }
    }
    task.resume()
} catch {
    print(error)
}

它打印:

[
    {
        "assetType": "video",
        "assetPath": "some_path"
    },
    {
        "assetType": "image",
        "assetPath": "some_other_path"
    }
]

----------------------------阿拉莫菲尔方式-------------- ------------

阅读文档 here显然,我也应该添加 encoding 选项:

Alamofire.request(
    "https://...",
    method: .post,
    parameters: [
        "assets": assets
    ],
    encoding: JSONSerialization(options: [])
)

这个问题将会得到解决。

关于ios - 发送到 Swift 中的 firebase 云函数后,字典对象数组会发生变化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50556467/

相关文章:

ios - Swift 3 中的 UILongPressGestureRecognizer

ios - 如何防止选择器 View 重复

ios - 从 .m4a 数据中过滤频率

ios - 将对象设置为参数时调用错误中的额外参数 'method'

swift - 在 RadarChartData 设置标签

node.js - Google App Engine - Node : Cannot find module 'firebase-admin'

iphone - Apple ID 可以访问吗?

iphone - 如何将时区偏移缩写转换为官方时区标识符?

javascript - Firebase 函数 admin.database() 事务中的 null 参数

firebase - 脱机尝试后,Firestore访问中断