我的理解是,在 guard 语句中用逗号分隔的两个条件函数强加了它们都为真的要求。我可以独立地编写一个保护语句,代码可以编译,但是当我将它们与逗号组合时,它会出错。我的语法有什么问题吗?或者谁能解释为什么编译失败?
guard (mode != "mapme") else { //compiles
}
guard (!annotation is MKUserLocation) else { //compiles
}
guard (mode != "mapme",!(annotation is MKUserLocation)) else { //gives error:'(Bool, Bool)' is not convertible to 'Bool'
}
最佳答案
你使用了太多无意义的圆括号,基本上不要在 if
和 guard
语句中的简单 表达式中使用圆括号。
错误发生是因为编译器将括起来的括号视为元组 ((Bool, Bool)
),这就是错误消息所说的。
guard mode != "mapme" else {
guard !(annotation is MKUserLocation) else { // here the parentheses are useful but the `!` must be outside of the expression
guard mode != "mapme", !(annotation is MKUserLocation) else {
关于swift - Swift 中具有两个条件的 Guard 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54172762/