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 = 42 myVariable = 50 let myConstant = 42 // 類型推導 let explicitDouble : Double = 70 // Swift 不支持隱式 類型轉換 (所以需要顯式類型轉換) let label = "The width is" let width = 94 let width1 = label + String(width) // 使用 \(item) 的形式進行 字符串格式化 let apples = 3 let orages = 5 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[1] = "bottle of water" var dict = [ "name": "melody", "age" : "26", ] dict["sex"] = "female" // 一般使用初始化器(initializer)語法創建空數組和空字典 let emptyArray = String[]() let emptyDict = Dictionary<String, Float>()
Xcode貼圖