【问题标题】:To call the cloud function from iOS app从 iOS 应用调用云功能
【发布时间】:2014-12-09 12:27:43
【问题描述】:

我开发了云计算前一天投票数的功能。

// 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 示例。请帮帮我!

更新。 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

【问题讨论】:

    标签: javascript ios swift parse-platform


    【解决方案1】:

    这里有一些调用云代码函数的快速代码。

    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.
      }
    }
    

    【讨论】:

    • 使编译器崩溃((
    • @Maga 也许传入一个空字典作为参数。你的编译器的错误信息是什么?它不会让我的编译器崩溃......
    • 2014-12-12 12:19:52.399 Rating[4082:166266] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[__NSPlaceholderDictionary initWithObjects:forKeys: count:]: 尝试从 objects[1]' 中插入 nil 对象 *** 第一次抛出调用堆栈:( 0 CoreFoundation 0x012d3946 __exceptionPreprocess + 182
    • @Maga 这看起来不像是编译器崩溃。它看起来像运行时崩溃。你能用你为调用函数而实现的代码更新你的问题吗?
    • @Maga 啊,看来你肯定需要传入一些参数。检查更新的代码块。
    猜你喜欢
    • 2021-06-20
    • 2021-11-20
    • 2020-01-28
    • 2021-11-06
    • 2021-12-15
    • 2017-06-29
    • 2019-11-25
    • 2018-09-06
    • 1970-01-01
    相关资源
    最近更新 更多