1
我想弄清楚为什么swift返回的是我在函数的开头声明的0,而不是返回从Parse中设置的数字。Swift返回一个值在函数之前Parse可以返回一个值
func getPhotosSubmitted(quest: String) -> Int{
var num = 0
var query = PFQuery(className:"Quest")
query.whereKey("questName", equalTo:quest)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
// NSLog("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
for object in objects {
num = (object["numOfSubmittedPhotos"] as Int)
println("---\(num)")
}
} else {
// Log details of the failure
//NSLog("Error: %@ %@", error, error.userInfo!)
}
}
return num
}
本功能将返回0每次,因为如果没有等待:num = (object["numOfSubmittedPhotos"] as Int)
当我尝试返回(object["numOfSubmittedPhotos"] as Int)
它抛出一个错误,说int是无法转换为无效 我打印测试线看我从Parse得到什么值,它是不是0的正确值。有没有人在这段代码中看到问题?
问题标题已经提示问题:函数返回_before_解析可以得到一个值。你必须重新设计它 – 2014-10-28 02:16:39
我该怎么做?我觉得它会等到它获得值,然后在像大多数语言一样返回之前将它插入到变量num中。 – user3290950 2014-10-28 02:29:05
'findObjectsInBackgroundWithBlock'这个方法名称告诉你soomething吗?你需要学习如何进行异步编程 – 2014-10-28 02:30:59