Simple experience
// 定义变量var i = 10println(i)i = 15println(i)let j = 20// 常量一经定义不能自改数值// j = 25println(j)
- Periodic summary
varDefine variables that can be modified after they are set
letDefines constants that cannot be modified after they are set
- Do not use at end of statement
;
- In Swift, use
println() an alternative in OCNSLog
printlnPerformance is better and will be demonstrated later
Defined
OCObject
//materialized view let v = uiview (frame: cgrectmake (0, 0, 100, 100)) //set background color V.backgroundcolor = uicolor.redcolor () //added to root view View.addsubview (v)
- Periodic summary
- In the
Swift format that you want to instantiate an object 类名() with, the OC equivalent in alloc/init
OCinitWithXXX Swift can be used to 类名(XXX: ) find the corresponding function in the
OCA [UIColor redColor] class method in which Swift you can usually 类名.XXX find the corresponding function in the
- Use
let adornments v and assign values to indicate该常量的内存地址不允许修改,但是可以修改其内部的属性
- The properties of the current object, which do not need to use
self.
Constants & Variables Use principle: Try to use let first, only need to change, then use Var, can be more secure
Variable type
let x = 10 let y = 10.5let z: double = 20println ( Double (x) + y) println (x + int (y)) println (y + z)
- Periodic summary
- In the first contact, the
Swift var let Swift type is very loose because of a simple mistake.
- In fact, the exact type of all variables is automatically deduced at the same time as the assigned value.
Swiftis a very strict type of language,一个值永远不会被自动转换成其他类型
- If you want to convert, you must display the conversion in Swift
- Decimal default is
Double type
- Integer default is
Int type
- If you want to explicitly specify the type of a variable, you can define it by using the
var 变量名: 类型 = 值
1. Constants & Variables