let ,常數,編譯的時候不需要有值,但只能給一次值。
var,變數
編譯器會自動判斷value的類型,例如
var myVariable = 42
編譯器會自動判斷myVariable為整數如果要指定值的類型,要在數值名稱後面加上類型,用冒號區隔。
let explicitDouble: Double = 70
Swift不會自動轉換類型,因此要強制轉換類型,
let label = "The width is"
let width = 94
let widthLabel = label + String(width)
轉換成String還可以用\(Variable)let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
Array和Dictionary的使用方法
var shoppingList = ["catfish", "water", "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
Array和Dictionary的初始化方法let emptyArray = String[]()
let emptyDictionary = Dictionary<String, Float>()
沒有留言:
張貼留言