简体   繁体   English

在Swift 1.2中解析复合查询

[英]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 和Xcode向我显示错误

Cannot invoke 'orQueryWithSubqueries' with an argument list of type '([PFQuery?])' 无法使用类型为([[PFQuery?]))的参数列表调用'orQueryWithSubqueries'

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. 除非设计不当,否则您使用的API会返回可选参数是有原因的。 Unwrap your optional safely using if let . if let使用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!])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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