1. Definition
let
Define constants that are not allowed to be modified once they are assigned, more secure
var
Define variables that can still be modified after assignment
- You should select constants as much as possible before you need to modify them to
var
- In Xcode 7.0, if the variable is not modified, Xcode will prompt to change to
let
Code Demo
ImportUIKit
ClassViewcontroller:Uiviewcontroller{
OverrideFuncViewdidload () { Super.Viewdidload() Demo() } FuncDemo () { //Define a constant and assign a value directly LetA =10
//Constants have been assigned values,Cannot be modified,Otherwise it will be an error A =20
//Define the constant type in the assignment first //Yellow warningSwiftThe new features,You are prompted to use this constant,If not applicable,Recommend that you use"_"To replace LetB:Int b =20
//Defining variables Varc =20 // variable definition can still be modified Value C = 30 } } |
2. Data typeThe default type of integer default type is int long decimal is double cgfloat single precisionshortcut key: Option+caps Lock View Interpretation3. Automatic derivationThe type of data in Swift is deduced automatically, depending on = right to determine the specific type of data
- Swift can deduce the exact type of the variable according to the code on the right.
- Typically at development time, you do not need to specify the type of the variable
- If you want to specify a variable, you can use it after the variable name:, and then follow the class of the variable
4. Type conversionSwift is a very strict type of validation language different types of data cannot be calculated directlyimplicit type conversions are not supportedto perform a calculation, you must make a forced type conversion
//Forcing type conversions Letx2 =100 let y2 = 10.5 let double (x2) + y2 Let num2 = x2 + Int(y2) |
5. Output function PrintFormat: Print (object)6. Specification writing code: Operators need to add spaces apart
Constants and variables for 3.Swift