Swift里变量用var
,常量用let
来定义
1 2 3 4 5 6 7 8 |
var a = 10 a = 20 var b = 100 let c = a + b println(c) //120 c = 100 //error:Cannot assign to 'let' value 'c' |
变量声明后可以随时改变值,而常量只能赋值一次,再赋值则会直接报错
Swift里变量用var
,常量用let
来定义
1 2 3 4 5 6 7 8 |
var a = 10 a = 20 var b = 100 let c = a + b println(c) //120 c = 100 //error:Cannot assign to 'let' value 'c' |
变量声明后可以随时改变值,而常量只能赋值一次,再赋值则会直接报错