由于现在Swift的语法更新的有些快,并且官方文档的例子和Xcode 6.1.1出来的结果不一样,所以我想着要把我学习Swift语言时的一些细节分享出来~~
1.简单值Let和Var
let: 用来定义常量
var: 用来定义变量
var a = 2
a = 1
let b = 1
// 打印出来的结果:1,1
ps:常量和变量赋值必须要和它们的类型一致。
2.给变量或者常量设置特定的类型
let a = 2
let b = 3
let c:Double = 70
// 打印出来的结果:2,3,70.0
这样子c的类型就是Double类型了
3.隐式声明和显式声明:
// 显示声明
let label = "The width is"
let width = 94
let widthLabel = label + String(width)
// 打印出来的结果:The width is94
//隐式声明
let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
// 打印出来的结果:I have 3 apples. I have 8 pieces of fruit.
4.创建数组和字典
var shoppingList = ["catfish","water","tulips","bluepaint"]
shoppingList [1] = "bottle of water"
println(shoppingList)
// 打印出来的结果: [catfish,bottle of water,tulips,bluepaint]
var occupations = [
"Malcolm": "Captain","Kaylee": "Mechanic",]
occupations["Jayne"] = "Pu blic Relat ions"
println(occupations)
// 打印出来的结果: [Kaylee: Mechanic,Jayne: Pu blic Relat ions,Malcolm: Captain]
5.创建一个空的数组或者字典
let emptyArray = [String]()
let emptyDictionary = Dictionary<String,Float>()
也可以用上面的例子直接生成一个空的数组
shoppingList = [] // 打印出来的结果: []
好了,这次我们就讲到这里,下次我们继续~