如何使用 .whereKey 修改查询,但在 Xcode 中使用多个不同的参数?
How to modify query with .whereKey, but utilizing multiple different parameters in Xcode?
我正在使用 swift 在 Xcode 中创建一个应用程序,它从解析服务器数据库(由 Heroku 托管)中提取信息。有一个方便的修改查询的方法,可以这样设置,例如:
let getFollowedUserQuery = PFQuery(className: "Followers")
getFollowedUserQuery.whereKey("Follower", equalTo: (PFUser.currentUser()?.objectId)!)
但是,我希望查询基于多个参数进行查询。目前它只检查数据库中的一列。有什么方法可以使用 .whereKey(或类似的东西)修改此查询,以便它将检查数据库中的多个 parameters/columns。本质上,它将根据用户输入的搜索参数检查这些列。但是用户可以 select 多个参数...所以查询只需要 return 适合 所有 参数的对象,而不仅仅是一个。有办法吗?
你检查过 docs 了吗?它说:
You can give multiple constraints, and objects will only be in the
results if they match all of the constraints. In other words, it’s
like an AND of constraints.
你的情况是:
let getFollowedUserQuery = PFQuery(className: "Followers")
getFollowedUserQuery.whereKey("Follower", equalTo: PFUser.currentUser()?.objectId ?? "")
getFollowedUserQuery.whereKey("AnotherKey1", equalTo: anotherObject1)
getFollowedUserQuery.whereKey("AnotherKey2", notEqualTo: anotherObject2)
另一种选择是使用 NSPredicate
:
let predicate = NSPredicate(format: "Follower = '\(PFUser.currentUser()?.objectId ?? "")' AND AnotherKey1 = '\(anotherObject1)' AND AnotherKey2 != '\(anotherObject2)'")
let query = PFQuery(className: "Followers", predicate: predicate)
我正在使用 swift 在 Xcode 中创建一个应用程序,它从解析服务器数据库(由 Heroku 托管)中提取信息。有一个方便的修改查询的方法,可以这样设置,例如:
let getFollowedUserQuery = PFQuery(className: "Followers")
getFollowedUserQuery.whereKey("Follower", equalTo: (PFUser.currentUser()?.objectId)!)
但是,我希望查询基于多个参数进行查询。目前它只检查数据库中的一列。有什么方法可以使用 .whereKey(或类似的东西)修改此查询,以便它将检查数据库中的多个 parameters/columns。本质上,它将根据用户输入的搜索参数检查这些列。但是用户可以 select 多个参数...所以查询只需要 return 适合 所有 参数的对象,而不仅仅是一个。有办法吗?
你检查过 docs 了吗?它说:
You can give multiple constraints, and objects will only be in the results if they match all of the constraints. In other words, it’s like an AND of constraints.
你的情况是:
let getFollowedUserQuery = PFQuery(className: "Followers")
getFollowedUserQuery.whereKey("Follower", equalTo: PFUser.currentUser()?.objectId ?? "")
getFollowedUserQuery.whereKey("AnotherKey1", equalTo: anotherObject1)
getFollowedUserQuery.whereKey("AnotherKey2", notEqualTo: anotherObject2)
另一种选择是使用 NSPredicate
:
let predicate = NSPredicate(format: "Follower = '\(PFUser.currentUser()?.objectId ?? "")' AND AnotherKey1 = '\(anotherObject1)' AND AnotherKey2 != '\(anotherObject2)'")
let query = PFQuery(className: "Followers", predicate: predicate)