【问题标题】:How to check if PFQuery result is true in Swift如何在 Swift 中检查 PFQuery 结果是否为真
【发布时间】:2015-12-05 03:42:25
【问题描述】:

在我的应用程序中,用户可以在注册时选择三种不同的帐户类型:父用户、家庭帐户和子帐户。我在 Parse 的用户数据库中添加了一个名为“isParent”的列,并将其设置为布尔值。现在,在 AppDelegate 中,我想检查“isParent”是否为真,这样我就可以显示某个 ViewController。我有以下代码:

在didFinishLaunchingWithOptions 内的AppDelegate.swift 中:

    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{

我收到以下错误消息:

键入“PFQuery?”没有下标成员

所以,我希望能够检查“isParent”是否为真,以便我可以打开视图控制器。

我该如何解决这个问题?

谢谢。

【问题讨论】:

    标签: ios swift parse-platform pfquery pfuser


    【解决方案1】:

    首先您需要触发查询以获取数据,然后您可以检查您的条件。

    您可以使用 PFQuery 类的方法来获取数据。

    findObjectsInBackgroundWithBlock
    findObjectsInBackgroundWithTarget:selector:
    getFirstObject 
    getFirstObjectInBackground ....etc.
    

    你可以选择你的方法...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-18
      • 2013-05-09
      • 2014-04-13
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      相关资源
      最近更新 更多