How to use playground to learn swift language is described in the previous blog, "Snooping on the playground of Swift programming ." This blog uses playground to spy on the swift language. A journey begins, of course, unless you are on a high-speed train or plane. Or that sentence. Starting with the basics, this blog introduces the basic data types of Swift. The type of data in Swift is a flourishing one, so let's look at the basic data types in Swift.
I. Variables and constants in Swift
1. Keyword "let"
Constants, as the name implies, the value of a constant can not be changed again, for example, you give number an initial value of "Swift", then the moment you assign it to a value is "swift". Just as you are born to be a man or a woman, you can't turn from a real man to a real woman, and vice versa. Of course the day after tomorrow if you go to Thailand that will not forget, if the day after tomorrow, the compiler will error, said you this is a shemale, is compiled not past. Talking and running off the point, after the use of the Let keyword in swift to create a constant and assign the initial value, then the value of the constant can not be changed, if you try to change it in use, then the compiler will really error. Constants can be defined in OC and C + + with a key const, while Swift uses let.
The following code snippet defines a constant named gender, and is assigned a value of "I am a Man", which will be followed by a gender value on the right side of playground, and the following sentence is an attempt to assign a value to gender, saying that you are going to Thailand and that you are not allowed to do so in the Swift world, So give you a mistake "cannot assign to ' let ' value ' gender '", is not allowed you to gender again, with a plain words is, do not allow you to Thailand that what. This is the constant in swift, final, Yiyanjichu, irrevocable is let's style.
2. Keyword "var"
If anything in the world is immutable, it's not going to live anymore, is it? Don't want to be a programmer's doctor is not a good boss, these can become the right, the world is still a few things, most things are moving, are changing, in the Swift world is the same, the use of variables more than constants. For example, you are now a programmer, tomorrow will be the boss, if this thing is constant, that is finished, do a lifetime of programmers, but in real life can become, the following is the use of keyword var to define a Hopeme variable, and assign "I am a programmer", you inspirational to be the boss, one day you did the boss, Then it becomes the following code, which is possible in Swift's world, so the compiler doesn't give error.
3. Description of the data type
The attentive garden-friends will notice that either the definition constants or the variables do not specify the data type for these variables or constants. Is swift like JS or php the same as the weak type of language? Non-also, not weak type, Swift can automatically infer the data type, which is defined by the string type. The following example defines a variable intnumber, assigns an initial value of 10, uses the Sizeofvalue () function to view the number of bytes of memory that the variable occupies, and uses the sizeof () function to view the storage space required for the corresponding data type. A better approach is to look at the data type of the hint when you type the variable.
Second, explicitly specify the corresponding data type to the variable
Whether the above is a declaration of constants or variables, there is a compiler through type inference to determine the type of data, then we will explicitly specify the type of data we need. The data types in Swift are colorful.
1. Explicitly declaring integer types of different bytes, allowing you to specify the number of bytes required for the data store in Swift. Of course Int64 is used on 64-bit systems to account for 64 bytes.
2. Give a simple example of life: different species are not able to combine to produce normal offspring, if you tell the compiler that +lv=? The compiler will make a mistake, saying that you are illegal and will produce an abnormal descendant "errormessage = Luo". No, it's not a salty egg. In swift, it is not allowed to perform implicit operations on different data types. Numbers of different data types are not allowed for arithmetic operations, and even if they are integral types, they are not allowed to be arithmetic in swift if the number of bytes stored is different. Some of the official swift languages are type-safe, and different types of data are not allowed to perform arithmetic operations directly, and the following example illustrates this problem well.
3.Swift is also very good support for different number systems, in order to readability, Swift allows you to separate the larger numbers as shown, 0b for binary, 0o for 8, and 0x for hexadecimal.
Iii. floating-point type in Swift
1. Other programming languages also have floating-point types, and the following Dmeo gives the number of bytes required for different types of floating-point type to be stored in memory. The default is the double type in swift.
2. The following demo shows the calculation of the class float type, the data of type float is not allowed to operate directly with the double type of data, or the sentence: implicit type conversion is not allowed in Swift.
3. The following is the exponential representation of the floating-point number, as shown below
Basic data types to spy on Swift