Notes and expressions in swift

Source: Internet
Author: User
Swift programs have two types of Annotations: single-line and multi-line annotations (/*...*/). The annotation method is similar to C, C ++, and objective-C. The following describes in detail.
1. Single Row comment
A single-line comment can be used to annotate a part of an entire line or a row. It is generally not used to annotate text that contains multiple consecutive rows. Of course, it can also be used to annotate consecutive multi-row code segments. The following are examples of two annotation styles:
If X> 1 {// comment 1} else {return false; // Note 2} // If x> 1 {// Note 1 //} else {// return false; // Note 2 //} indicates that you can use the shortcut key to annotate multiple consecutive lines of text in xcode: Select multiple lines and press the "command +/" key to annotate. To remove the comment, press the "command +/" key. 2. Block annotations are generally used for comments of multiple consecutive lines, but you can also annotate individual lines. The following are examples of annotation styles: If x> 1 {/* Comment 1 */} else {return false; /* Comment 2 */}/* If x> 1 {// comment 1} else {return false; // Note 2} * // * If x> 1 {/* Comment 1 */} else {return false;/* Comment 2 */}*/


It is prompted that swift multi-line comments have the advantage that other languages do not have, that is, they can be nested. In the last case of the above example, multi-line comments are nested.
In program code, it is necessary to comment out codes that are easy to misunderstand, but do not comment out codes that have clearly expressed information. Note that frequent comments sometimes reflect the low quality of the Code. When you feel forced to add comments, consider rewriting the Code to make it clearer.
Expression
Expressions are an important part of program code. In swift, expressions are in three forms.
1. do not specify the Data Type
VaR a1 = 10
Let A2 = 20
VaR A = A1> A2? "A1": "A2"
In the above Code, we directly assign values to variables or constants without specifying the data type, because the data type can be automatically inferred in swift.
2. Specify the Data Type
VaR A1: Int = 10
Let A2: Int = 20
VaR A = A1> A2? "A1": "A2"
In the above Code, int specifies the data type for variables and constants. This method makes the program readable. We recommend specifying the Data Types of variables and constants.
3. Use a semicolon
VaR A1: Int = 10; var A2: Int = 20
VaR A = A1> A2? "A1": "A2"
In swift, you can add semicolons (;) after a statement ends. However, you must use semicolons (;) when multiple statements are written in one line, you need to use semicolons to differentiate statements. For example:
VaR A1: Int = 10; var A2: Int = 20;


For more information, please refer to the first domestic swift book "swift development guide" for discussion. Website: http://www.51work6.com/swift.phpwelcome to the swifttechnology discussion group: 362298.pdf

Welcome to Zhijie IOS public classroom Platform



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.