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")

}

3Powerful:

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)")

}


4Interactive

5Fast。

-------------------------------------

基本数据类型:

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>()


下一篇文章将介绍控制语句