swift 中的空格规则是什么
what are the rules for spaces in swift
我在 swift 的操场上练习,但我不明白为什么 swift 对程序员应该在何处提供空格而在何处不提供过于具体。我在很多网站和聊天室问过这个问题,但没有得到任何答案。
var j: Int = 34 // Right
var j:Int=23 //Wrong
还有,在class
self.variable-= 5 //Wrong. Error: Consecutive statements must be followed by ;
self.variable-=5 // Right
self.variable -= 5 // Right
;
即使是这个“:”有时也会产生一些空格问题。
我认为空格对代码绝对没有影响。它通常只是为了程序员的利益。它只是使代码更具可读性。阅读有关空格的所有 swift 规则的最佳资源是什么。
可以在此处找到问题第二部分的答案swift docs
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).
etc... (read the docs for more on this)
至于你问题的第一部分,我没有发现任何一种声明变量的方式有任何问题。
var j: Int = 34
var j:Int=23
所提供代码的唯一问题是您在同一范围内声明了 j 两次。尝试将其中一个 j 更改为 x 或 y 或其他内容。
如果您想知道
var j:Int =10
或
var j:Int= 10
看看上面的规则。 = 是一个运算符,因此如果您要执行其中任何一个操作,它将被视为前缀或后缀,并且您会收到 prefix/postfix = is reserved
的错误
由于存在一元加号和一元减号运算符等运算符,这些规则很重要。编译器需要能够区分二进制加号和一元加号运算符。 List of operators
我在 swift 的操场上练习,但我不明白为什么 swift 对程序员应该在何处提供空格而在何处不提供过于具体。我在很多网站和聊天室问过这个问题,但没有得到任何答案。
var j: Int = 34 // Right
var j:Int=23 //Wrong
还有,在class
self.variable-= 5 //Wrong. Error: Consecutive statements must be followed by ;
self.variable-=5 // Right
self.variable -= 5 // Right
;
即使是这个“:”有时也会产生一些空格问题。
我认为空格对代码绝对没有影响。它通常只是为了程序员的利益。它只是使代码更具可读性。阅读有关空格的所有 swift 规则的最佳资源是什么。
可以在此处找到问题第二部分的答案swift docs
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).
etc... (read the docs for more on this)
至于你问题的第一部分,我没有发现任何一种声明变量的方式有任何问题。
var j: Int = 34
var j:Int=23
所提供代码的唯一问题是您在同一范围内声明了 j 两次。尝试将其中一个 j 更改为 x 或 y 或其他内容。
如果您想知道
var j:Int =10
或
var j:Int= 10
看看上面的规则。 = 是一个运算符,因此如果您要执行其中任何一个操作,它将被视为前缀或后缀,并且您会收到 prefix/postfix = is reserved
的错误由于存在一元加号和一元减号运算符等运算符,这些规则很重要。编译器需要能够区分二进制加号和一元加号运算符。 List of operators