Swift Learning------constants and variables

Source: Internet
Author: User
Tags constant definition

Constants and variables

* 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

10

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

Ten

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)

Ten20.1

note: If you do not specify the type of the expression and do not give the initial value, the compiler will error

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.1Let Number7 =Tenlet Number8=10.1//let sum = number7 + Number8Let sum = Double (number7) +Number8let sum1= Number7 +Int (number8)//operations between literals can be of different typesLet sum2 =Ten+10.1//cgfloat/doubleLet point = Cgpoint (x:10.10Y:20.20) Let temp=Point.xlet 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.

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.1)//: tuples by Subscript. 0 Tuples. 1 // : Specify the element name  about 3.14 ) tuples1.xxxtuples1.ooo//: Decomposition element let (intvalue, Doublevalue) = (998  4.1) Intvaluedoublevalue

Swift Learning------constants and variables

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.