ios - 使用 Swift 解析来自 Firebase Cloud Function 的 FIRHTTPSCallableResult 对象数据

标签 ios json swift firebase google-cloud-functions

我有这个简单的云功能:

export const getTasks = functions.https.onRequest((request, response) => {
    admin.firestore().collection('tasks').get()
    .then(snapshot => {
        const results = []
        snapshot.forEach(task => {
            const data = task.data()
            results.push(data)
        })
        response.send(results)
    })
    .catch(error => {
        console.log(error)
        response.status(500).send(error)
    })
});

来自浏览器的 https 调用给了我一个正确的 json:

[
{
title: "A title",
dueDate: "2018-07-03T18:33:27.537Z",
isComplete: true,
type: "task",
date: "2018-07-02T18:33:27.537Z"
},
{
type: "task",
date: "2018-07-02T18:36:25.506Z",
title: "Wowo",
dueDate: "2018-07-02T21:59:59.000Z",
isComplete: true
},
{
title: "Abc",
dueDate: "2018-07-04T18:31:58.050Z",
isComplete: false,
type: "task",
date: "2018-07-02T18:31:58.050Z"
}
]

但是当我尝试通过该函数从 iOS 客户端接收数据时,我得到一个 FIRHTTPSCallableResult 对象和一个 nil 对象:

functions.httpsCallable("getTasks").call() { (result, error) in
            if let error = error as NSError? {
                if error.domain == FunctionsErrorDomain {
                    //...
                }
                // ...
            }

            print( "result -> \(type(of: result))")
            print( "result?.data -> \(type(of: result?.data))")

日志:

result -> Optional<FIRHTTPSCallableResult>

result?.data -> Optional<Any>

我尝试使用 JSON 解析,但它不起作用。如何获取 json?

谢谢

最佳答案

API documentation for the data field状态:

The data is in the form of native objects. For example, if your trigger returned an array, this object would be an NSArray. If your trigger returned a JavaScript object with keys and values, this object would be an NSDictionary.

由于您要从函数发送对象数组,因此您会将 data 的内容视为 NSDictionary 对象的 NSArray .

关于ios - 使用 Swift 解析来自 Firebase Cloud Function 的 FIRHTTPSCallableResult 对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51181504/

相关文章:

swift - 使用 SwipeCellKit 自定义 TableViewCell

ios - 如何在 UITextview 中的每一行前添加破折号 (-)?1

ios - 一个 Controller 中的 UITableView && UICollectionView?

php - jQuery 获取 JSON 网址

javascript - jQuery 不会正确地发布包含数组作为其属性之一的 JSON

ios - 转换 [UInt8]?印象

ios - 用嵌套 block 保留 self ?

ios - 使用 CoreData 进行单元测试 DI 不起作用

json - 使用模板、参数文件和 powershell 创建可用性 Web 测试 | New-AzApplicationInsightsWebTest

Swift UIControlState.Selected 保持选中状态吗?