简体   繁体   中英

Parse Compound Queries in Swift 1.2

I have this query

var postsExploreQuery = Post.query()
postsExploreQuery!.whereKey("isPrivate", equalTo: false)

var query = PFQuery.orQueryWithSubqueries([postsExploreQuery])
query.whereKey("isPublished", equalTo: true)

return query

and Xcode shows me error

Cannot invoke 'orQueryWithSubqueries' with an argument list of type '([PFQuery?])'

what I'am doing wrong :(

You really should get out of the habit of putting ! after all your optionals. This removes all the safety that optionals are intended to give you. Unless it has been poorly designed, there's a reason the API you are using will return an optional. Unwrap your optional safely using if let . This removes the chance that your program will randomly crash in the future and also gives you the chance to handle the error with an else if it makes sense for your program.

var postsExploreQuery = Post.query()
if let postsExploreQuery = postsExploreQuery {
    postsExploreQuery.whereKey("isPrivate", equalTo: false)

    var query = PFQuery.orQueryWithSubqueries([postsExploreQuery])
    query.whereKey("isPublished", equalTo: true)

    return query
}

我的猜测是, orQueryWithSubqueries需要一个非可选数组,因此您可能必须将其编写为:

var query = PFQuery.orQueryWithSubqueries([postsExploreQuery!])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM