从 iOS 应用程序调用云函数 [英] To call the cloud function from iOS app
本文介绍了从 iOS 应用程序调用云函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经开发了云功能来统计最后一天的投票数.
I've developed the cloud function to count the number of the votes within last day.
// 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 示例.请帮帮我!
I deployed the code successfully. But how to develop SWIFT code to call the function? Unfortinately no Swift example for that purpose at Parse.com. Please help me!
更新.1. 我正在使用@rickerbh 建议的代码
Upd. 1. I'm using the code suggested by @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
推荐答案
这里是一些调用云代码函数的 swift 代码.
Here's some swift code that calls cloud code functions.
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.
}
}
这篇关于从 iOS 应用程序调用云函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文