Cats learn Swift variables and constants (2)

Source: Internet
Author: User

Cat Share, must boutique

Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243#

Constants: Let

Let is a keyword, a language keyword, used to declare constants, what is a keyword? GA do not want to explain, Java Public,oc in the beginning of the @, JS in the Var and so on. )
constants, which are often used (as you understand), are like men, women, π=3.14, etc., which are common in life. These cannot be changed, and the constants in swift cannot change, so remember this is enough.

//常量:let20

If you want to change the constants, you will get an error.

Calculation of constants
let1    //整数默认类型是Intlet0.8  //小数默认类型是Doublelet num = num1 + num2

The above code will cause an error because Swift is a type-specific voice that is not implicitly convertible under any circumstances.

But what do we have to do about it? As long as you follow the same type principle.

let1    //整数默认类型是Intlet0.8  //小数默认类型是Double//let num = num1 + num2let num3 = Double(num1) + num2let num4 = num1 + Int(num2)

It is important to note that double int is not rounded, but is discarded after the decimal point.

Variable var

Variables are declared with Var. After the declaration can be modified

var v = 20v = 30

But note that you can only change the same type. With object-oriented thinking, the initialization is equivalent to birth, and after a pig is born you can't make him a person ... Can't just become a person can change that is eight commandments ...

Tip, we cannot copy the string type to the int type, which means that Swift is a very strict type of language. (a value will never be automatically converted to another type)

Display definition type, conversion type

Definition is to use
var variable name: type = value
Let variable name: type = value
You can display the definition type, such as

var a:Double = 3

We can press and hold the keyboard option and click on the variable A to see his type, when you can see his type is double

, although we did not do a display type, Swift would automatically deduce that he is a double type

Conversion type

var3var b = (Int)(a)

, we force the conversion type, and the initialized B automatically derives the int type.

Summarize:

1: Variable var can be modified after initialization
2: Solid let cannot be modified after initialization
3: Note: In the daily development, should be preferred let, in the need to modify at this time, in the Var

Cats Learn Swift Variables and constants (2)

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.