Ios 使用Swift从Firebase Cloud函数解析FirHttpCallableResult对象数据
我有一个简单的云函数:Ios 使用Swift从Firebase Cloud函数解析FirHttpCallableResult对象数据,ios,json,swift,firebase,google-cloud-functions,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 da
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客户端接收数据时,我得到一个FirHttpCallableResult对象和一个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))")
日志:
结果->可选
结果?.data->可选
我尝试使用JSON解析,但不起作用。如何获取json
感谢各州:
数据采用本机对象的形式。例如,如果您的
触发器返回一个数组,此对象将是一个NSArray。如果你的
触发器返回一个包含键和值的JavaScript对象,该对象
那将是一本词典
由于您要从函数中发送一个对象数组,因此将
数据的内容
视为NSDictionary
对象的NSArray
。嘿,道格,谢谢!无法将“HttpScalableResult”类型的值分配给类型“[String:Any]”,或者如果我这样做:array=result?。数据为![String:Any]应用程序与此响应一起崩溃,它工作:var result={“data”:array};响应。发送(结果)
result -> Optional<FIRHTTPSCallableResult>
result?.data -> Optional<Any>