以下代码调用错误。我在引用资料中找不到关于此的任何信息。 “=”运算符右侧缺少空格是一个错误。
let names =["Anna", "Alex", "Brian", "Jack"]
编译此语法的任何其他组合。根据我们目前对 Swift 的了解,有人知道这是否真的是无效语法吗?
编辑:错误响应是:Prefix/postfix '=' is reserved
答案:这段摘录似乎回答了我的问题。只是找了半天没找到:
The whitespace around an operator is used to determine whether an operator is used as a prefix operator, a postfix operator, or a binary operator. This behavior is summarized in the following rules:
If an operator has whitespace around both sides or around neither side, it is treated as a binary operator. As an example, the + operator in a+b and a + b is treated as a binary operator. If an operator has whitespace on the left side only, it is treated as a prefix unary operator. As an example, the ++ operator in a ++b is treated as a prefix unary operator. If an operator has whitespace on the right side only, it is treated as a postfix unary operator. As an example, the ++ operator in a++ b is treated as a postfix unary operator. If an operator has no whitespace on the left but is followed immediately by a dot (.), it is treated as a postfix unary operator. As an example, the ++ operator in a++.b is treated as a postfix unary operator (a++ . b rather than a ++ .b).
摘自:Apple Inc.“The Swift Programming Language”。电子书。 https://itun.es/us/jEUH0.l
最佳答案
在=
后加一个空格。 (=[
看起来很难成为一个运算符。)它可能将 =value
视为对(可能但未实现)前缀运算符的使用。
Swift 并不像 C 那样完全与空白无关……特别是,它使用空白来区分前缀和后缀运算符(因为 C 中的 ++i++
是一种语法怪癖)。但它也不像 Python 那样对空格严格得离谱。
关于swift - 编译器的这个响应有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24436863/