【发布时间】:2015-05-17 16:14:24
【问题描述】:
在我的 Xcode 项目中,我有一个名为 Question 的类,带有实例变量:
import Foundation
class QuestionModel {
let question: String?
let answers: [String]
}
我在 parse.com(命名为 Question)数据库上也有一个类,其中包含我创建的一个对象,我添加了三列(一个字符串名称问题、一个名为答案的字符串数组和一个名为问题日期的字符串,以便我可以访问它)。
在我的视图控制器上,我想用解析中的数据创建一个 Question 类的实例:
let newQuestion = QuestionModel()
var query = PFQuery(className:"Questions")
query.whereKey("date", equalTo:"5/17/15")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
newQuestion.options = object["options"] as? [String]
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
println(newQuestion.options)
它显示它找到了 1 个对象 但是当我添加最后一个 println 时,它打印为零。为什么不打印我在 Parse 数据库中添加的问题字符串?
【问题讨论】:
-
最后一个 printLn 首先运行,甚至在查询开始之前。块参数中的代码在查询完成后运行。在该块运行之前没有要打印的内容。
-
感谢您的回答!但是我怎样才能让查询和块代码在我使用问题的实例之前运行呢?
-
仅在块内使用它,或者从您从块中调用的某些方法中使用它。
标签: ios swift parse-platform ios8