ios - 如何在 Swift 中检查 PFQuery 结果是否为 true

标签 ios swift parse-platform pfquery pfuser

在我的应用程序中,用户可以在注册时选择三种不同的帐户类型:家长用户、家庭帐户和 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/

相关文章:

ios - Swift JSONDecoder 解码(类型 :from) does not recognize my data

ios - TableView 之上的 UiView

ios - 使用 Swift 进行 PubNub 和解析?

ios - 添加 subview myNavigationController.view 时关闭 20 像素(y 轴)

ios - 加载自定义外观值

iphone - 在 iPhone/iPad 中发送一个 http 请求的电池消耗是多少?

swift - 删除场景 View 节点后应用程序崩溃

swift - 继承自@objc协议(protocol)

ruby-on-rails - 如何根据 Parse REST API 中的 session token 检索用户?

ios - 转换为字符串的任何对象