ios - 为什么反转函数参数的 bool 结果会在 Swift 中产生编译器错误?

标签 ios swift

我刚刚开始学习 swift,但这对我来说毫无意义。这段代码:

func myFlipper(f: Int -> Bool, num:Int) {
    return !f(num)
}

导致编译器错误“Cannot invoke '!'带有 'Bool' 类型的参数”。

来自 Apple 的 swift 文档“逻辑 NOT 运算符 (!a) 反转 bool 值,使 true 变为 false,false 变为 true。” link

有人知道这是怎么回事吗?

最佳答案

此函数具有 void 类型,您将返回 Bool。如果你想返回 Bool 写成,

func myFlipper(f: Int -> Bool, num:Int) -> Bool{
   return !f(num)
}

否则不要返回任何东西,它也不会给出任何警告。

关于ios - 为什么反转函数参数的 bool 结果会在 Swift 中产生编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26024816/

相关文章:

ios - 如何降低键盘高度

ios - 核心数据: Null Array when retrieving Data Base objects

ios - 创建IOS 3D效果

ios - swift ,在同一 View 中显示一个警报然后显示另一个警报

swift 2.0 : Parametrized classes don't call proper == function if it inherits from class that is Equatable

swift - 如何将 CollectionView 单元格彼此相邻对齐?

ios - 当 UIScrollView 在 ViewController 上显示时 UIButton 不显示

ios - 我可以使用 Xcode 8.3.2 将用 Swift 4 编写的代码提交到 App Store 吗?

ios - 如何防止在自定义segue中堆叠 View ?

IOS 构建良好但在设备上运行时出错