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

时间:2022-02-25 09:51:31

I'm trying to create a Cloud Code function which will return multiple values. Now what I'm doing is

我正在尝试创建一个返回多个值的Cloud Code函数。现在我正在做的是

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

And according to the Cloud Code logs everything going well. But what I don't know is what kind of object I should cast it when I handle function in my Swift code.

根据Cloud 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. If so, is there any other way of returning multiple values from Cloud Code function?

我也有一个想法,我可能是一个错误的方法,因为我基本上试图找到一个javascript对象的Swift等价物。如果是这样,还有其他方法可以从Cloud Code函数返回多个值吗?

I have the following in my console log when I println the object.

当我打印对象时,我在控制台日志中有以下内容。

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

2 个解决方案

#1


0  

Ok, then your Object is an Dictionary Optional... You can use this Code:

好的,那么你的对象是一个字典可选...你可以使用这个代码:

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

#2


0  

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

应该管用

#1


0  

Ok, then your Object is an Dictionary Optional... You can use this Code:

好的,那么你的对象是一个字典可选...你可以使用这个代码:

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

#2


0  

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

应该管用