我正在尝试创建自己的自定义运算符!!
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/