swift - 编译器的这个响应有效吗?

标签 swift

以下代码调用错误。我在引用资料中找不到关于此的任何信息。 “=”运算符右侧缺少空格是一个错误。

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/

相关文章:

ios - Swift:在 UIAlertController 中添加 UITextView

objective-c - 从 swift 调用 Objective-C 宏

ios - 从 ViewController 传回数据

ios - 通知发出声音但不显示任何内容

json - 下载后如何打印自定义 JSON 对象值?

ios - 如何在 UIView 中持续更新绘图

ios - 如何从 Collection View 中的不可见单元格中删除自定义 View

swift - 如何在现有 UIKit 类(如 UIColor)的扩展中添加初始值设定项?

swift - ObjectMapper toJSON() 快速

ios - 如何向 Root View 而不是 TopLayoutGuide 添加约束?