Some days ago. After the first swift developers Conference was opened. A lot of friends around OC can not restrain.
Think it's time to learn swift, after all, Swift is a trend.
It was also their repeated request, let me tidy up the swift learning experience. Today, starting with Swift's basic grammar,
definition
letDefine constants, once assigned, do not agree to change
varDefines a variable. Can still be changed after assignment
//define constants and set values directly LetX:Int=Ten//constant value once set, can not be changed, the following code will error//x =//Use ': type '. Only the type is defined. Without setting a value LetY:Int//constants have an opportunity to set values once. There is no problem with the following code. Because ' Y ' has not been set to a valuey =Ten//Once the value has been set. You cannot change it again. The following code will error, because ' Y ' has been set the value//y =Print(x + y)//variable can continue to change value after setting the valuevarZ:Intz = -z = $Print(x + y + z)
self-motivated deduction
- Swift can deduce the exact type of the variable according to the code on the right.
- Usually at development time. You do not need to specify the type of the variable
- Suppose you want to specify a variable. You can use it after the variable name
: , and then follow the type of the variable
No implicit conversions
- Swift is extremely strict with data type requirements
- No matter what time. It's not going to be an implicit conversion.
Let & var options
- The constants should be chosen as much as possible , only if they have to be changed to
var
Swift Foundation 1.1--Basic syntax-variables and constants