swift - 我怎么定义!!作为自定义后缀运算符?

标签 swift operators

我正在尝试创建自己的自定义运算符!!

postfix operator !! //error

static postfix func !! (optionalValue: Optional<T>) -> T {
        // realisation
    }

我收到错误信息

Expected operator name in operator declaration

在声明中。

最佳答案

自定义运算符只能使用有限的一组字符,有些运算符是保留的,不能重载。 精确的规则记录在 Lexical Structure 中.特别是(强调):

Although you can define custom operators that contain a question mark (?), they can’t consist of a single question mark character only. Additionally, although operators can contain an exclamation mark (!), postfix operators can’t begin with either a question mark or an exclamation mark.

还有

  • 你的运营商是通用的,所以你必须声明T作为 <T> 的占位符类型,
  • 函数不能是static除非在类型中定义。

工作示例:

postfix operator =!!

postfix func =!! <T> (optionalValue: Optional<T>) -> T {
    // realization
}

关于swift - 我怎么定义!!作为自定义后缀运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57704978/

相关文章:

ios - XCODE iphone 6 plus 和 6s plus 在显示 UIAlertViewController 时显示警告

ios - 使用从 Firebase 获取的数据填充 tableView

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

swift - 当我有默认值时,为什么 xcode 要我将 navigationController 设为可选?紧随其后的值(value)

c++ - this 和 const 深入

C# LuaInterface 类运算符

ios - 更改UItabbar顶部边框线的默认颜色

c++ - 为什么编译器告诉我在 'if'和 'else if'语句中没有运算符与操作数类型匹配?

c# - 什么是运营商 `|=` ?我如何在 C# 中实现它?

c++ - C++ 运算符如何工作