/* * let:声明常量 * var:声明变量 */ var myVariable =42 myVariable =50 let myConstant =42
// 如果初始值没有提供足够的信息(或者没有初始值),则需要在变量后面声明类型,用冒号分割。 let implicitInteger =70 let implicitDouble =70.0 let explicitDouble: Double=70
// 值永远不会被隐式转换为其他类型。如果你需要把一个值转换成其他类型,请显式转换。 let label ="The width is " let width =94 let widthLabel = label +String(width) // 删去String()会报错
// 有一种更简单的把值转换成字符串的方法:把值写到括号中,并且在括号之前写一个反斜杠(\)。 let apples =3 let oranges =5 let appleSummary ="I have \(apples) apples." let fruitSummary ="I have \(apples + oranges) pieces of fruit."
// 使用三个双引号(""")来包含多行字符串内容。每行行首的缩进会被去除,只要和结尾引号的缩进相匹配。 let quotation =""" I said "I have \(apples) apples." And then I said "I have \(apples + oranges) pieces of fruit." """
// 使用方括号 [] 来创建数组和字典,并使用下标或者键(key)来访问元素。最后一个元素后面允许有个逗号。 var shoppingList = ["catfish", "water", "tulips", "blue paint"] shoppingList[1] ="bottle of water" var occupations = [ "Malcolm": "Captain", "Kaylee": "Mechanic", ] occupations["Jayne"] ="Public Relations"