Knowledge Points:
- specifying the data type of a constant \ variable
- Swift Strict requirements variables must be initialized before they are used
- Maximum : You can use the Min and Max methods to get the most value
- Range: Values exceeding storage range Swift will error
- Two variables of different types cannot be added
- number format: You can add 0 (0) or underline (_) to enhance readability
- Type Conversions
- Use the Typealias keyword to define the alias of type in swift, similar to the typedef in C
Example code:
: Playground-noun:a place where people can playimport uikit//Swift common data types are://Int, Float, Double, Bool, Character, S tring//Array, Dictionary, tuple type (tuple), optional type (Optional)//and the first letter of all data types is uppercase//1, specified variable \ constant data type///after constant \ variable name plus colon (:) and type name let Age:int = 10//Defines an INT type constant age, with an initial value of 10//it is generally not necessary to specify the type of variable \ Constant, similar to the type mechanism of JavaScript//2, Swift strict requirements variables must be initialized before use//3, Max let MI Nvalue = Uint8.minlet MaxValue = uint8.max//4, Range//In Swift, if the value exceeds the storage range, the compiler will directly error//But in objective-c, the compiler is only warning, recommended instead of long/ /Let Num:uint8 = 2143243253242343243253242 This sentence is error//5, type is different two variables \ Constants cannot be added let Fnum:float = 3.14//Float type let Dnum = 3.2 2//double type//The following sentence is error, two types are different cannot add/let result = Fnum + dnum//need to cast to result = Double (fnum) + dnum//6, number format// Additional 0 (0) Let money = 001999let Money2 = 001999.000//can be added with additional underscore _ to enhance readability let OneMillion1 = 1_000_000let overonemillion = 1_ 000_000.000_001//7, type conversion let NUM1 = 3let num2 = 0.14//the types of NUM1 and num2 have been determined here, so the following must be type-cast make sum = Double (NUM1) + num2//but under The face is correct let sum2 = 3 + 0.14//since March 0.14 added after determining the type of sum2, so is correct//8, Swift uses the Typealias keyword to define the type of alias, similar to the role of typedef in C Typealias MyInt = Intlet Mynum:myint = 20let Minmynum = Myint.min
Swift Learning-data type