简体   繁体   English

Parse.com-从Cloud Code函数返回多个值

[英]Parse.com - Returning multiple values from a Cloud Code function

I'm trying to create a Cloud Code function which will return multiple values. 我正在尝试创建将返回多个值的Cloud Code函数。 Now what I'm doing is 现在我正在做的是

response.success({val1 : scores1, val2: scores2});

And according to the Cloud Code logs everything going well. 根据Cloud Code记录,一切进展顺利。 But what I don't know is what kind of object I should cast it when I handle function in my Swift code. 但是我不知道在处理Swift代码中的函数时应该投射什么样的对象。

    PFCloud.callFunctionInBackground("myFunction", withParameters: ["value" : 1) { (object:AnyObject?, error:NSError?) -> Void in
       //what I should cast object for
    }

I also have a thought it my mind that it can be a wrong approach as of I'm basically trying to find Swift equivavelnt to a javascript object. 我也想过,从我基本上试图找到与JavaScript对象对应的Swift等价物开始,这可能是错误的方法。 If so, is there any other way of returning multiple values from Cloud Code function? 如果是这样,还有其他方法可以从Cloud Code函数返回多个值吗?

I have the following in my console log when I println the object. 当我打印对象时,控制台日志中包含以下内容。

Optional({
poetry = 1;
rap = 9;
})

Just return one object that has properties for each return value or an array of values or a dictionary (or whatever floats your boat ;)) and deserialize it on the client. 只需返回一个对象,该对象具有每个返回值或值的数组或字典(或任何使您的船浮起的东西)的属性,然后在客户端上反序列化即可。 This way you only have one return value that contains all the data you need conveniently massaged in to an object/list/dictionary/whatever... 这样,您只有一个返回值,其中包含您需要方便地发送到对象/列表/字典/任何内容的所有数据。

PFCloud.callFunctionInBackground("myFunction", withParameters: ["value" : 1) { (object:AnyObject?, error:NSError?) -> Void in
    if let values = object as! [String:Int] {
    // Do something
    }
}

should work 应该管用

Ok, then your Object is an Dictionary Optional... You can use this Code: 好的,那么您的Object是一个Dictionary可选的...您可以使用以下代码:

if let responseJSON: [String: Int] = object as? [String: Int] {
      let poetryValue: Int = object["poetry"] as Int
      let rapValue: Int = object["rap"] as Int
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM