欢迎来到Swift天地,Welcome to Swift

期待已久的WWDC真的是不管是什么硬件更新,没有太多的开发者,本次会议是还是很有亮点。水果给我们带来了一种新的语言Swift。种无比简洁高效的语言,并且新的 Swift 语言依然会和 C 与 Object-C 相兼容。

原文地址:Welcome to Swift

欢迎来到swift的世界

Swift是水果公司推出的新型面向对象语言,用于iOS和OS X平台开发。

它是一种现代的(modern)、功能强大的、易于使用的语言。

以下的代码段创建了一个字典并遍历、打印全部元素:

let people = ["Anna": 67, "Beto": 8, "Jack": 33, "Sam": 25]

for (name, age) in people {

println("\(name) is \(age) years old.")

}

安全性:

Swift的类型推导(type inference)机制是类型安全的。Swift限制了对指针的直接訪问。而且自己主动管理内存。是它可以更easy的搭建安全、稳定的软件。

func configureLabels(labels: UILabel[]) {

let labelTextColor = UIColor.greenColor()

for label in labels {

// label inferred to be UILabel

label.textColor = labelTextColor

}

}

现代化:

Swift涵盖了(optionals)、泛型(generics)、元组(Tuple)等其它现代语言的特性。它的启示和改进使objective-c和swift的代码使用起来更自然

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

}

功能强大:

Swift拥有强大的模式匹配功能。可以高速编写简单的、表现力强的代码。Format strings 让字符串的操作更自然。在Swift中使用像Foundation、UIKit之类的框架也非常直接。

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

}

交互式的

引入playgrounds具,我们可以用它来试验新技术、问题分析、而设计界面原型

高速

Swift编译器采用了先进的代码分析技术来调整代码的性能,让我们把重点放在如何建立一个伟大的app相反,在复杂的系统优化浪费时间