在我的应用程序中,用户可以在注册时选择三种不同的帐户类型:家长用户、家庭帐户和 child 帐户。我在 Parse 的用户数据库中添加了一列名为“isParent”的列,并将其设置为 bool 值。现在,在 AppDelegate 中,我想检查“isParent”是否为 true,以便我可以显示某个 ViewController。我有以下代码:
在 AppDelegate.swift 中的 didFinishLaunchingWithOptions 中:
if PFUser.currentUser() == nil {
// code to show sign up/login goes here
}
} else {
let query = PFUser.query()
query?.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)
if query["isParent"] == true{ // error here. see below.
// parent view controller
print("this user is a parent")
}
} else{
// non parent view controller
print("this user is not parent")
}
}
正如您在上面所读到的,错误位于:
if query["isParent"] == true{
我收到以下错误消息:
Type "PFQuery?" has no subscript members
所以,我希望能够检查“isParent”是否为真,以便我可以打开 View Controller 。
我该如何解决这个问题?
谢谢。
最佳答案
首先,您需要触发查询来获取数据,然后您可以检查您的条件。
您可以使用PFQuery类的方法来获取数据。
findObjectsInBackgroundWithBlock
findObjectsInBackgroundWithTarget:selector:
getFirstObject
getFirstObjectInBackground ....etc.
您可以选择您的方法...
关于ios - 如何在 Swift 中检查 PFQuery 结果是否为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34101002/