Swift learn the constants and variables of note one

Source: Internet
Author: User

This series of notes is for bloggers to watch the Liuyubobobo instructor-led "play swift2.0" classroom study notes and organize the class of the code, but also through the web app to watch his video support him, the instructor of all the classroom code on GitHub, As well as updated swift3.0 code, suggest or go through the teacher's video first, and then knock the class code, and then in turn to review, I believe there will be a lot of help.

In the OC language, a variable is indicated by its type before it is declared, such as

NSString *name = @ "Liuyubobobo";

However, in swift type, the variables of course also have the specified type, but at the time of declaration, they are divided into two types: constant and variable.

Constants cannot be changed after they are declared and initialized, and the variable is the value of the quantity that can be constantly changed.

Constants are declared with the Let keyword, and variables are declared with the VAR keyword

Let Maxname = 1000var index = 2

The swift language deliberately refers to the concept of constants to the same height as variables, in fact encouraging developers to think about whether a quantity is a constant or a variable. Most of the time, a volume is no longer changed after the declaration, and this time Apple suggests that we declare this amount as let, so that this amount consumes less of the entire computing resource, and the entire program is optimized.

The constant of the Maxname declaration above, if we assign it in the playground to 2000, then the compiler error, and can automatically change the Let keyword to var. With this feature, unless we have a very definite variable life purpose, many times we can safely declare a quantity as a constant let, and this amount will be modified in the future code, then can be modified directly through the compiler to Var.

Another point, unlike other languages, is that Swift does not require a semicolon to be enforced after each statement, and the swift language does not write a semicolon at all, unless you explicitly add a semicolon when you write multiple-day statements on a single line, and of course, Apple does not recommend that we write multiple statements on one line unless there is a very clear need.

We find that each volume is prefixed with a let or var keyword, so what type are they? In fact, the swift language has a type inference mechanism (Tyoe inference), that is, by initializing the value to infer the actual type of the volume, for example, in the above example, two is inferred by Swift as an int type, you can use the option key to see the amount of the real type. Swift language is a strongly typed language, and many times we do not need to explicitly indicate the type of a volume like any other language. The swift language also supports types that display the amount of land declared, such as

Let website:string = "Www.imooc.com" var A, B, c:double

Swift learn the constants and variables of note one

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.