Wind notes-swift programming language: Point1~5

Source: Internet
Author: User
Tags constant definition

Catalog Index

Breeze Annotated-swift programming language

Point 1.

Swift-style "Hello, World"

code Example:

println ("Hello,World")  

Annotations:

    • The code style of the Swift language is similar to C or objective-c. Therefore, people with C or OBJECTIVE-C development experience are more likely to master the swift language.
    • In Swift, there is no code contained in any class or function, and it belongs to the code that owns the global scope.
    • You do not need to import a separate library for input or output or string processing.
    • The println function is one of the swift language built-in functions. Swift consists of 74 built-in functions.
    • Swift's statement does not need to end with a semicolon, which is the recommended practice, but also to simplify the code and reduce the amount of code input.
    • You can still add semicolons at the end of the statement. In fact, in some cases, semicolons are still required, such as for loop statements, where multiple statements are entered in a row.

Point 2.

The "main" function in Swift

code Example:

Func main () {}

Annotations:

    • The swift language does not resemble the main function in C or objective-c.
    • The Swift Language program writes the code in the global scope to the entry point of the program.
    • Even if the user customizes a function named Main, the program will still not be like C or objective-c, the main function as the entry point of the program, but the same as the normal function to deal with the main function.

Point 3.

Constants and variables in Swift

code Example:

1 //constant Definition2Let address ="1th Wangfujing Street, Dongcheng District, Beijing"3Let Iphone6price:int =52884 5 //variable definition6var pagenumber =1247 var airtemperature:float8var stockprice:double =5.889var employeename:string ="Zhang San"
View Code

Annotations:

  • Declaratively: Constants are declared with the Let keyword, and variables are declared with the VAR keyword.
  • The value of a constant cannot be changed once it is set, and the value of the variable can be changed arbitrarily.
  • 4888         // Error: The value of a constant cannot be changed once it is set 6.24           // correct: The value of the variable can be changed arbitrarily
  • The types of constants or variables must be the same as the values you assign to them.
  • " XXX "      // error: The type of a constant or variable must be the same as the value you assign to it 3275       // correct: The type can be assigned the same value
  • When you declare a constant or variable, its type is optional. If you do not specify a constant or a variable type, the compiler automatically infers the type.
  • " 1th Wangfujing Street, Dongcheng District, Beijing "   // type is inferred as String 124                    // type is inferred as int
  • When declaring constants, you must provide an initial value.
  • " 1th Wangfujing Street, Dongcheng District, Beijing "   // A constant definition must be assigned a value 5288           // A constant definition must be assigned a value
  • When declaring a variable, the initial value is not required, but the variable type must be supplied when no initial value is provided.
  • var airtemperature:float  //  variable declaration does not have to assign an initial value, but the type must be dimensioned at this time  
  • If the initial value does not provide enough information (for type inference), you need to declare the type after the constant or variable, separated by a colon.
  •  + // If you do not label the float type, the compiler infers the value to be of type int   
  • Values are never implicitly converted to other types. If you need to convert a value to another type, explicitly convert it.
  • " The width is "  94= Label + string (width) //  Displays the value of width converted to string type
  • There is a simpler way to convert a value to a string: write the value in parentheses, and write a backslash before the parentheses.
  • 35"I have \ (apples) apples. "  "I have \ (apples + oranges) pieces of fruit. "

Point 4.

Type callout

code example:

5288" Zhang San "

Annotations:

    • When you declare a constant or variable, you can add a type callout (type annotation), indicating the type of the value to be stored in a constant or variable.
    • If you are adding a type callout, you need to add a colon and a space after the constant or variable name, and then add the type name.
    • Without a constant or variable of type callout, the compiler will automatically infer the type.

Point 5.

Names of constants and variables

Annotations:

  • There is no limit to Swift's constant and variable name length.
  • You can use any character you like as a constant and variable name, including Unicode characters
  • 3.14159" Hello World ""dogcow"
  • Constants and variable names cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode code points, lines and tabs.
  • // The following declarations are either constant or variable declarations that are wrong 3 Let xxx " XXX "   -
  • Constants and variable names cannot begin with a number, but they can contain numbers elsewhere in a constant and variable name.
  •  -     // a valid variable name  $     // illegal variable names, constants and variable names cannot begin with a number
  • Once you declare a constant or variable as a deterministic type, you cannot declare it again with the same name, or change the type of value it stores.
  • // The following declarations are either constant or variable declarations that are wrong // within the same scope, the declared constant or variable name cannot be declared again.  " Zhang San   "" John Doe "1024x768
  • If you must use the same name as the SWIFT reserved keyword as a constant or variable name, you can use the inverse quotation mark (') to enclose the keyword in the same way as the name.
  •  //  use swift reserved word as constant or variable name 123"test"   
  • You cannot turn constants and variables into each other.

Wind notes-swift programming language: Point1~5

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.