Swift 是什么,大家都回去百度或者Google,有的甚至认为是Taylor Swift(她是我的偶像),但是如果今天在百度百科里搜索绝对没有说是Apple最新推出的编程语言,因为是在2014年6月3日凌晨1点多(北京时间)在2014年WWDC上发布的,它会在未来逐步替代Objective-C开发语言,这让我们学了Objective-C的人又要学习新的编程语言,给大家看张图片:这是大家的感受!
但是我们还要去学习Swift!毕竟未来不落后这个时代!
Swift是什么?
Swift是苹果于WWDC 2014发布的编程语言,这里引用The Swift Programming Language的原话:
Swift is a new programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility.
Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible and more fun.
Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch frameworks, is an opportunity to imagine how software development works.
Swift is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.
简单的说:
- Swift用来写iOS和OS X程序。(估计也不会支持其它屌丝系统)
- Swift吸取了C和Objective-C的优点,且更加强大易用。
- Swift可以使用现有的Cocoa和Cocoa Touch框架。
- Swift兼具编译语言的高性能(Performance)和脚本语言的交互性(Interactive)。
Swift语言概览
// Playground - noun: a place where people can play import Cocoa var str = "Hello, playground"
var str1 = "Hello Wrold!!!"
var str2 = "O(∩_∩)O哈哈~" // Hello, world
println("Hello, world") // 变量与常量
// Swift 使用 var 声明 变量 , let 声明常量
var myVariable =
myVariable =
let myConstant = // 类型推导
let explicitDouble : Double = // Swift 不支持隐式 类型转换 (所以需要显式类型转换)
let label = "The width is"
let width =
let width1 = label + String(width) // 使用 \(item) 的形式进行 字符串格式化
let apples =
let orages =
let sum = "I have \(apples) apples."
let sum1 = "I have \(apples + orages) pieces of fruit." // 数组和字典
// Swift 使用[] 操作符声明 数组(array)和字典 (dictionary)
var listArr = ["fish","water","apple","rice"]
listArr[] = "bottle of water" var dict = [
"name": "melody",
"age" : "", ]
dict["sex"] = "female" // 一般使用初始化器(initializer)语法创建空数组和空字典 let emptyArray = String[]()
let emptyDict = Dictionary<String, Float>()
看看我在Xcode 6上编程的效果: