let a = 12 // 申明a为常量12,类型Int let b: Double = 12 // 申明a为常量12,手工指定类型Double
let str = "Hello, playground. " let name = "Tom " let count = 10 let say = str + name + String(count) // "Hello, playground. Tom 10"
let a = 1 let b = 2 let say = "the number is \(a + b)" // "the number is 3"
var arr = ["catfish", "water", "tulips", "blue paint"] arr[1] = "bottle of water" // 修改第二项 arr // ["catfish", "bottle of water", "tulips", "blue paint"]
var man = [ "like": "apple", "name": "Tom", ] man["iphone"] = "5s" // 增加字段 man["like"] = "mac" // 修改字段 man // ["iphone": "5s", "like": "mac", "name": "Tom"]
四、控制语句
1、if 不可以直接使用一个变量或者常量来作为条件,必须是判断语句let a = 12 if a > 0 { // do something } else { // do something }
for i in 0..3 { // i = 0, 1, 2 } for var i = 0; i < 3; ++i { // i = 0, 1, 2 } // 数组 let scores = [1, 2, 3, 4, 5] for score in scores { // score } // 字典 let numsDic = [ "a": [2, 3, 5, 7, 11, 13], "b": [1, 1, 2, 3, 5, 8], "c": [1, 4, 9, 16, 25], ] for (key, numbers) in numsDic { for number in numbers { // number } }
var n = 2 while n < 100 { n = n * 2 } var m = 2 do { m = m * 2 } while m < 100