从iOS应用程序调用云功能

时间:2014-12-09 12:27:43

标签: javascript ios swift parse-platform

我已经开发了云功能来计算最后一天内的投票数量。

// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:

Parse.Cloud.define("confidenceRating", function(request, response) {
    // To define vars yesterday and today
    var today = new Date();
    var yesterday = today.setDate(today.getDate() - 1);

    // "score" is the table's name       
    var query = new Parse.Query("score");

    // filter the query, 2 filters to be applied
    query.greaterThanOrEqualTo("createdAt", yesterday);
    query.lessThan("createdAt", today);

    // perfom the action
    query.find({
        success: function(results) {
            var votes = results.length;
            response.success(votes);
        },
        error: function() {
            response.error("something went wrong");
        }
    });
});

我成功部署了代码。但是如何开发SWIFT代码来调用函数呢?令人遗憾的是,在Parse.com上没有为此目的的Swift示例。请帮帮我!

UPD。 1.我正在使用@rickerbh建议的代码

PFCloud.callFunctionInBackground("confidenceRating", withParameters: nil) { results, error in
  if error != nil {
    // Your error handling here
  } else {
    // Deal with your results (votes in your case) here.
  }
}

但是我收到了错误

2014-12-12 12:19:52.399 Rating[4082:166266] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]' *** First throw call stack: ( 0 CoreFoundation 0x012d3946 __exceptionPreprocess + 182

1 个答案:

答案 0 :(得分:0)

这里有一些调用云代码功能的快速代码。

let parameters = ["": ""]
PFCloud.callFunctionInBackground("confidenceRating", withParameters: parameters) { results, error in
  if error != nil {
    // Your error handling here
  } else {
    // Deal with your results (votes in your case) here.
  }
}