swift - Swift 中具有两个条件的 Guard 语句

标签 swift mkannotation guard

我的理解是,在 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'

}

最佳答案

你使用了太多无意义的圆括号,基本上不要在 ifguard 语句中的简单 表达式中使用圆括号。

错误发生是因为编译器将括起来的括号视为元组 ((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/

相关文章:

swift - 如何实现符合具有类型约束的协议(protocol)的通用基类并为这些通用类型实现工厂?

ios - Gamekit 的 GKOctree 找不到元素

ios - MKAnnotationView 和点击检测

iphone - MKAnnotation:iOS 5.0 中的标题

ruby-on-rails - 守卫运行规范三遍

ruby 守卫 : Is the directory being watched stored in some variable?

ios - Xcode 错误连接到字段

ios - Google maps Places Picker for iOS - Swift 中的 GMSPlace Picker 出现但在动画结束时消失

iPhone map View 自定义标注

ruby - 输入 "guard"会出现异常