使用let来声明常量,使用var来声明变量
Variable
- Oc
- Define re-initialize first
int num; num = 10
- Simultaneous initialization of the definition
int num = 20
- Swift
- Define re-initialize first
var num
- Error: No data type specified (type annotation missing in pattern), if you want to define a variable in swift, you must tell the compiler variable's type (type callout) when you use it later.
var num: Intnum = 10
- 定义的同时初始化 - 在Swift中如果定义的同时初始化一个变量,可以不用写数据类型, 编译期会根据初始化的值自动推断出变量的类型(其它语言是没有类型推断的) - 以后在开发中如果在定义的同时初始化就没有必要指定数据类型, 除非需要明确数据类型的长度或者定义时不初始化才需要指定数据类型
var num2: Int = 20var num3 = 20
You can use any character you like as a constant and variable name, including Unicode characters, constants and variable names that cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode code points, lines and tabs. You cannot start with a number, but you can include a number elsewhere in a constant and variable name. ”
var ?? = 100var 哈哈 = 300
var 3x = 10var x+-3 = 10
Constant:
- oc:const int num = 10;
- Swift:let num = 10
- Error:
let num : Int
- Constants in Swift must be initialized at the time of definition (OC can be uninitialized) or an error will be
- Use of constants: Some values do not need to be changed
let num4 = 10
Constants and variables