您好,登錄后才能下訂單哦!
Today ,蘋果剛剛發布了Swift語言,我們來看下Swift的幾個主要特性:
1 Safe
func configureLabels(labels: UILabel[]) {
let labelTextColor = UIColor.greenColor()
for label in labels {
// label inferred to be UILabel
label.textColor = labelTextColor
}
}
2 Modern
let cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]
let sortedCities = sort(cities) { $0 < $1 }
if let indexOfLondon = find(sortedCities, "London") {
println("London is city number \(indexOfLondon + 1) in the list")
}
let size = (20, 40)
switch size {
case let (width, height) where width == height:
println("square with sides \(width)")
case (1..10, 1..10):
println("small rectangle")
case let (width, height):
println("rectangle with width \(width) and height \(height)")
}
4 Interactive
5 Fast。
-------------------------------------
基本數據類型:
1 簡單屬性:
let: 恒定常量
var:可變常量
1.1 定義常量不用指定其固定類型,編譯器會處理,如需更多識別,請添加前綴:
let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70
println("int:\(implicitInteger) double:\(implicitDouble) explicitDoubledss:\(explicitDouble)");
2 類型之間的轉化,必須是顯示的:
let label = "width is :";
let width = 54;
let widthLabel = label + String(width)
println("LabelWidth:\(widthLabel)");
3 使用反斜杠在String中插入內容,代碼參考2
4 使用中括號創建數組和字典:
var shoppingList = ["catfish", "water", "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
println("array is \(shoppingList)")
println("dictionary is :\(occupations)")
//create Empty
let emptyArray = String[]()
let emptyDictionary = Dictionary<String, Float>()
下一篇文章將介紹控制語句
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。