Constants and variables

Source: Internet
Author: User
Tags constant definition

/*:
Constant variable
* Let constant: The value of a constant cannot be changed once set
* var variable: The value of the variable can be changed arbitrarily
* Constants & Variables Use principle:
* To ensure the security of the data as far as possible first let, only need to change the time to use Var
*/

/*:
Constant definition Format
* Declaration Symbol constant Name: type callout
* Let Number:int
* Features: cannot be changed once set
*/
Let Number:int = 10
Number = 20

/*:
Defining variable formats
* Declaration Symbol variable name: type callout
* Var number:int
* Features: Variable values can be changed arbitrarily
* Note: In general, you rarely need to write type callouts. If you assign an initial value when declaring a constant or variable, swift can infer the type of the constant or variable
*/
var number2:int = 10
Number2 = 20

/*:
Type inference
* The compiler can automatically infer the type of the expression when compiling the code. (Other languages are not type inferred)
* The principle is simple, just check your assigned value can
* If no type of expression is specified and no initial value is given, the compiler will error (type annotation missing in pattern)
*/
Let Number3 = 10
var number4 = 20.1

Note: If you do not specify the type of the expression and do not give the initial value, the compiler will error
var number5:int
NUMBER5 = 88

/*:
Type safety
* Swift is a type safe language that does type checking when compiling your code (type checks) and marks the mismatched type as an error. This allows you to find and fix errors as early as possible during development
* Swift has strict data type requirements, and Swift does not do ' implicit conversion ' at any time, and if you want to calculate different types of data, you must display the type conversion
Attention:
* Combining numeric class constants and variables differs from combining numeric literals. The literal 8 can be added directly to the literal 3.1415926, because the number literal itself does not have a definite type. Their type is only inferred when the compiler requires a value
* Double and cgfloat also need to be converted
*/

Let Number6:int = 10.1
Let Number7 = 10
Let Number8 = 10.1
Let sum = Number7 + Number8
Let sum = Double (number7) + Number8
Let sum1 = Number7 + Int (Number8)

Operations between literals can be of different types
Let sum2 = 10 + 10.1

Cgfloat/double
Let point = Cgpoint (x:10.10, y:20.20)
Let temp = point.x
Let sum3 = Double (temp) + Number8

/*:
Names of constants and variables
* You can use any character you like as a constant and variable name, including Unicode characters
* Constants and variable names cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode code points, lines and tabs
* Also cannot start with a number, but can contain a number elsewhere in a constant and variable name
* ... As with C and OC, no Zuo no die

Attention:
* If you need to 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 a way that uses it as a name.
* In any case, you should avoid using keywords as constants or variable names unless you have no choice.
*/

Let?? = "Dog"
Let ' Int ' = 10


/*:
Ganso
* Tuples (tuples) combine multiple values into a single composite value
* To enclose multiple values of the same or different types in a single parenthesis is a primitive.
* Values within tuples can be of any type and do not require the same type
* tuples are useful as function return values

Attention:
* Tuples are useful when organizing values temporarily, but are not suitable for creating complex data structures. If your data structure is not used temporarily, use a class or struct rather than a tuple
*/

Let tuples = (10, 10.1)

: by subscript
tuples.0
Tuples.1

: Specify the element name
Let tuples1 = (xxx:99,ooo:3.14)
Tuples1.xxx
Tuples1.ooo

: Explode elements
Let (intvalue, doublevalue) = (998, 4.1)
Intvalue
Doublevalue

Constants and variables

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.