Swift Learning Record-Basics

Source: Internet
Author: User

Basics:

  • Constants and variables
  • Note
  • Semicolon
  • Integer
  • Floating Point Number
  • Type security and type inference
  • Numeric literal
  • Numeric type conversion
  • Type alias
  • Boolean Value
  • Tuples
  • Optional
  • Assertions

Declare constants and variables

Use let to declare constants, and use VaR to declare variables.

Type Annotation

When declaring a constant or variable, you can add type annotation to indicate the type of the value to be stored in the constant or variable.

If you want to add a type annotation, you need to add a colon and space after the constant or variable name, and then add the type name.

var welcomeMessage: String

Note: Generally, you rarely need to write type annotations. If you assign an initial value when declaring a variable or constant, Swift can infer the type of the variable or constant.

Constant and variable naming

Constants and variable names cannot contain mathematical symbols, arrows, reserved Unicode characters, connections, and tabs. It cannot start with a number, but it can contain numbers elsewhere in the constant and variable name.

Once you declare a constant and a variable as a definite type, you cannot declare it again with the same name or change the type of the stored value. At the same time, you cannot convert constants and variables.

Note:

If you need to use the same name as the reserved keyword of swift as the constant or variable name, you can use backquotes to enclose the keyword as the name.

 

Output constants and variables

The println function outputs the value of the current constant or variable:

Println (friendlywelcome) // output "Bonjour! "

Println is a global function used for output. The output content is wrapped in a line break.

 

Swift uses string interpolation to add a constant or variable name to a long string as a placeholder. Swift replaces these placeholders with the values of the current constant or variable. Place the constant or variable name in parentheses and escape it with a backslash before the parentheses:

Println ("the current value of friendlywelcome is \ (friendlywelcome)") // output "the current value of friendlywelcome is Bonjour!
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.