swift中的空格规则是什么?

时间:2023-01-23 18:11:30

I practicing in swift's playground and I couldn't figure out why swift is too specific about where programmer should provide spaces and where not. I asked this question on many sites and chatrooms but didn't got any answer.

我在swift的操场上练习,我无法弄清楚为什么swift过于具体,程序员应该提供空间和不提供空间。我在很多网站和聊天室都问过这个问题,但没有得到任何答案。

var j: Int = 34 // Right
var j:Int=23 //Wrong

Also, In class

另外,在课堂上

self.variable-= 5 //Wrong. Error: Consecutive statements must be followed by ;
self.variable-=5 // Right
self.variable -= 5 // Right

;

Even this ":" creates some issues with spaces sometimes.

即使这个“:”有时会产生一些空间问题。

I think spaces should have absolutely no effect on the code. It's usually just for a programmer's benefit. It just makes the code more readable nothing else. What's the best resource to read all swift rules about spaces.

我认为空格应该对代码完全没有影响。这通常只是为了程序员的利益。它只是使代码更具可读性。什么是阅读所有关于空间的快速规则的最佳资源。

1 个解决方案

#1


13  

Answer to the second part of your question can be found here swift docs

回答你问题的第二部分可以在这里找到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.

如果运算符在两侧或两侧都有空格,则将其视为二元运算符。例如,a + b和a + b中的+运算符被视为二元运算符。

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.

如果运算符仅在左侧有空格,则将其视为前缀一元运算符。例如,++ b中的++运算符被视为前缀一元运算符。

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.

如果运算符仅在右侧有空格,则将其视为后缀一元运算符。例如,++ b中的++运算符被视为后缀一元运算符。

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).

如果运算符左侧没有空格但后面紧跟一个点(。),则将其视为后缀一元运算符。例如,++。b中的++运算符被视为后缀一元运算符(++ .b而不是++ .b)。

etc... (read the docs for more on this)

等...(阅读文档了解更多信息)

As for the first part of your question, I didn't see any issue with either way of declaring the variables.

至于你问题的第一部分,我没有看到任何一种声明变量的问题。

var j: Int = 34
var j:Int=23

The only issue with that provided code is that you declare j twice in the same scope. Try changing one of the j's to an x or y or something else.

提供代码的唯一问题是您在同一范围内声明了两次j。尝试将其中一个j更改为x或y或其他内容。

If you were wondering about

如果你想知道

var j:Int =10

or

var j:Int= 10

look at the rules above. = is an operator so if you were to do either of those, it would be treated as prefix or postfix, and you would get the error that prefix/postfix = is reserved

看看上面的规则。 =是一个运算符,所以如果你要做其中任何一个,它将被视为前缀或后缀,你会得到前缀/后缀=保留的错误

These rules are important due to the existence of operators such as the unary plus and unary minus operators. The compiler needs to be able to distinguish between a binary plus and a unary plus operator. List of operators

由于存在诸如一元加和一元减运算符之类的运算符,这些规则很重要。编译器需要能够区分二进制加号和一元加号运算符。运营商名单

#1


13  

Answer to the second part of your question can be found here swift docs

回答你问题的第二部分可以在这里找到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.

如果运算符在两侧或两侧都有空格,则将其视为二元运算符。例如,a + b和a + b中的+运算符被视为二元运算符。

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.

如果运算符仅在左侧有空格,则将其视为前缀一元运算符。例如,++ b中的++运算符被视为前缀一元运算符。

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.

如果运算符仅在右侧有空格,则将其视为后缀一元运算符。例如,++ b中的++运算符被视为后缀一元运算符。

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).

如果运算符左侧没有空格但后面紧跟一个点(。),则将其视为后缀一元运算符。例如,++。b中的++运算符被视为后缀一元运算符(++ .b而不是++ .b)。

etc... (read the docs for more on this)

等...(阅读文档了解更多信息)

As for the first part of your question, I didn't see any issue with either way of declaring the variables.

至于你问题的第一部分,我没有看到任何一种声明变量的问题。

var j: Int = 34
var j:Int=23

The only issue with that provided code is that you declare j twice in the same scope. Try changing one of the j's to an x or y or something else.

提供代码的唯一问题是您在同一范围内声明了两次j。尝试将其中一个j更改为x或y或其他内容。

If you were wondering about

如果你想知道

var j:Int =10

or

var j:Int= 10

look at the rules above. = is an operator so if you were to do either of those, it would be treated as prefix or postfix, and you would get the error that prefix/postfix = is reserved

看看上面的规则。 =是一个运算符,所以如果你要做其中任何一个,它将被视为前缀或后缀,你会得到前缀/后缀=保留的错误

These rules are important due to the existence of operators such as the unary plus and unary minus operators. The compiler needs to be able to distinguish between a binary plus and a unary plus operator. List of operators

由于存在诸如一元加和一元减运算符之类的运算符,这些规则很重要。编译器需要能够区分二进制加号和一元加号运算符。运营商名单