Swift error in for loop ' + + ' is not a binary operator

Source: Internet
Author: User



I have recently started to learn swift and have encountered a very strange problem. A syntax error occurred while writing the For loop. The code is as follows:


 
for var i = 0; i < 10; i++{
   println("hello world") 
}


This is arguably the simplest use of the For loop in Swift. But the compiler still reported two errors:


    1. ' + + is not a binary operator '
    2. Operator is not a known binary Operator


While Apple says in its official documentation that it is recommended to use the ++i operator in this way, it is still possible to find evidence that the + + operator is both a prefix operator (prefix operator) and a suffix operator (postfix operator). Depends on its position relative to the variable. Although it does not work exactly the same as the prefix operator and the postfix operator, there is no problem in writing the For loop.



Look closely at the apple Introduction "Lexical Structure" document, (sorry not to translate, let's call lexical structure it). Click to go to document address



There is a passage that introduces the prefix and suffix characteristics of operators, and extracts the following:


The whitespace around an operator are used to determine whether an operator are used as a prefix operator, a postfix operato R, or a binary operator. This behavior are summarized in the following rules:

If an operator have 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 have 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 have 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.


The first paragraph tells us that there is a set of rules for whether an operator is handled by the Swift compiler as either a prefix operator or a postfix operator. is based on the space around the operator.



The following three paragraphs explain how to define the prefix operator, the suffix operator, and the two-tuple operator, respectively.



In the first example of the article, note that our writing is i++{. There are no spaces around the + + operator, so it is defined as a two-dollar operator (binary operator). However, in the swift definition of the + + operator, it can only be used as a prefix operator or suffix operator. So the mistake happened.



Then why would you report a second error--operator is a known binary Operator?



Because the operator can be customized in Swift, after the Swift compiler fails to attempt to use the + + operator, it also attempts to understand the ++{as a custom operator, but since we have not implemented this operator, we will quote an error for the unknown operator.



There are two ways to solve this small problem, one is to use the ++i operation. The code is as follows:


 
 
for var i = 0; i < 10; ++i{
   println("hello world") 
}


Referring to the second rule, the + + operator is correctly recognized as the prefix operator. This approach is recommended because the official documentation recommends using the prefix operator when using the self-coding operator.



Or just add a space between the {and + + operators. Since I am used to the usage of i++, I prefer to use this solution, the code is as follows:


 
for var i = 0; i < 10; i++ {
   println("hello world") 
}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Swift error in for loop ' + + ' is not a binary operator


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.