Swift-Basic Data Type

Source: Internet
Author: User

I. constants and variables

The swift language clearly distinguishes constants and variables.

Use the let keyword to declare a constant:

Let maxnmber = 18 // declare the initial value of a constant as 18. The value of a constant cannot be changed,

Use the VaR keyword to declare a variable:

VaR number = 12 // declare a variable. The initial value is 12, and the value of the variable can be changed.

When declaring a variable or constant, no data type is specified. The Compiler automatically pushes down the data type. You can also specify the data type.

Specify the data type by adding the type name after the variable name

VaR number: Int = 12 // integer

VaR English: String = "abcdefg" // string type

To make digital expressions easier to read and more natural, the digital expression format black can contain additional information

For example:

Let number = 0000000_000

Constants or variables of different types in SWIFT cannot be operated directly. To perform an operation, you must perform an explicit type conversion. The format is generally "type name (constant/variable name )"

Let number1 = 78.5

Let number2 = 80

VaR sum = number1 + double (number2)

The literal can be operated directly.

Sum = 78.5 + 80

Ii. tuples

Tuples are a new data type provided by SWIFT. They are a combination of multiple values. A Multivariate group can be a combination of any type or even different types of values.

VaR statusinfo = (404, "notfound ")

Equivalent to VaR statusinfo :( int, string) = (404, "notfound ")

This multivariate group represents a combination of an integer and a string. When used, each separated value can be obtained from the multivariate group.

Use subscript to access specific elements

Println ("statusinfocode = \ (statusinfo.0), statusinfomessage = \ (statusinfo.1 )")

You can name each independent item in the tuples.

VaR statusinfo = (statusinfocode: 404, statusinfomessage: "notfound ")

Println ("The statusmessage is \ (statusinfo. statusinfomessage )")

Tuples

VaR (statusinfocode, statusinfomessage) = (404, "not found ")

Println ("stautsinfocode: \ (statusinfocode )")

Iii. Optional types

When an element has a value but the type is not sure, or there is no value, use

Let possiblenumber = "123"

Let convertednumber: Int? = Possiblenumber. toint ()

? The identifier indicates that the modified value is an option type, which means that the type value can contain valid integer values or contain no value.

Because the toint method may fail, the compiler infer that possiblenumber. toint () returns an optional int type. An optional int type is written in syntax: Int ?. A variable or constant of the Option type indicates that the constant or variable allows no value.

You can use the if statement to determine whether an option contains a value. if an option contains a value, it is evaluated as true; otherwise, it is evaluated as false. if you use the if statement to evaluate whether an option contains a value, you can add one after the option name! To obtain the value of this option. If you try to use it! To access a non-existent option value will trigger a runtime error, so you are using it! Before accessing the option value, always make sure that the option value contains a valid value, or use the above method for determination.

If you want to use a value of the optional type, you can use force resolution!

Println ("convertednumber: \ (convertednumber !) ")

It can be set to nil for optional types, indicating that no value exists (only available for optional types can be assigned nil)

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.