Swift語言

Swift
编程范型範式物件導向泛函指令式組塊英语Block_(programming)結構化
設計者克里斯·拉特納蘋果公司
實作者蘋果公司
发行时间2014年6月2日,​9年前​(2014-06-02[1]
当前版本
  • 5.10 (2024年3月5日;穩定版本)[2]
編輯維基數據鏈接
型態系統靜態、強型別類型推論
操作系统蘋果公司的操作系統(Darwin, iOS, iPadOS, macOS, tvOS, watchOS), Linux, Windows 10, Android
許可證Apache許可證2.0(Swift 2.2及以後版本)
專有(直到Swift 2.2)[3][4]
文件扩展名.swift
網站
啟發語言
Objective-CRustHaskellRubyPythonC#CLU[5],和D語言[6]

Swift編程語言,支持多编程范式編譯式,用来撰写基于macOS/OS XiOSiPadOSwatchOStvOSvisionOS的软件[7]。 苹果公司于2014年在苹果开发者年会(WWDC)發布了Swift编程语言。从设计上苹果公司让Swift与Objective-C共存在苹果公司的操作系统[7]

歷史[编辑]

Swift是为了替代苹果公司早期的Objective-C编程语言,该语言自20世纪80年代初以来基本没有任何更新,缺乏现代语言功能。

2010年7月,苹果开发者工具部门总监克里斯·拉特納开始着手 Swift 编程语言的設計工作,以一年時間,完成基本架構後,他領導了一個設計團隊參與其中,与苹果的许多程序员共同开发,借鉴了Objective-CRustHaskellRubyPythonC#CLU等许多其他语言的設計思想。[5]2014年6月2日,苹果在WWDC上发布了第一个公开的使用Swift编写的应用程序:WWDC应用程序,同时向苹果的注册开发者发布了Swift语言的测试版,但未承诺最终版本与测试版本的源代码相互兼容。大会也发布了长达500页的官方指南,The Swift Programming Language,在Apple Books和官方网站上免费提供。苹果计划在完整版发布时,如果有必要,将会提供源代码转换器。

苹果宣称Swift的特点是:快速、现代、安全、互动,而且明顯優於Objective-C语言。Swift以LLVM編譯,可以使用现有的CocoaCocoa Touch框架。Xcode Playgrounds功能是Swift为苹果开发工具带来的最大创新,该功能提供強大的互动效果,能让Swift源代碼在REPL撰寫过程中能即時显示出其執行結果。拉特纳本人强调,Playgrounds很大程度是受到布雷特·维克多英语Bret Victor理念的启发。[8]

2014年9月9日,Swift 1.0和iOS 8、Xcode 6.0的Golden Master版本一同发布,达成了一个重要的里程碑。

2015年6月8日,蘋果於WWDC2015上发布了Swift 2.0。同时宣布,未来Swift將開放原始碼,包括编译器标准库[9]2015年9月21日起,可在App Store发布以Swift编写的应用。[10]

Swift在2015年Stack Overflow开发者调查中荣获“最受欢迎的编程语言”第一名,[11]并在2016年荣获第二名。[12]

2015年12月3日,蘋果宣布開源Swift,並支持Linux,蘋果在新網站swift.org和託管網站Github上開源了Swift,但蘋果的App Store並不支持開源的Swift,只支持蘋果官方的Swift版本,官方版本會在新網站swift.org上定期與開源版本同步。[13][14]

2015年12月,IBM宣布推出Swift Sandbox网站,该网站允许开发人员在一个窗口中编写Swift代码,并在另一个窗口中显示输出。该网站于2018年1月被弃用。

Linux发行版Ubuntu自Swift 2.2开始提供Swift的官方下载。从Swift 5.2.4起,包括CentOSAmazon Linux在内的更多发行版加入其中。[15]亦有用于Android的非官方生成工具链和SDK。[16][17]

2016年6月13日,苹果在WWDC2016上宣布将推出一款iPad专属应用:Swift Playgrounds英语Swift Playgrounds。该程序以3D游戏的形式演示如何使用Swift编写程序,编写代码时会实时提供反馈。[18][19][20]

2017年1月,克里斯·拉特纳宣布离开苹果,并将加入特斯拉担任新职位。Swift项目负责人由团队资深人士泰德·克雷梅内克担任。[21][22]

2019年6月3日,苹果在WWDC2019上发布了SwiftUI英语SwiftUI,同时发布了Xcode 11,为苹果平台带来了声明式UI结构设计框架。[23]

平台[编辑]

Swift支持苹果操作系统(Darwin/macOSiOSiPadOStvOSwatchOSvisionOS)和LinuxWindowsAndroid系统。

Swift的一个设计关键在于,它能够与过去十几年的大量现有Objective-C代码(如Cocoa框架和Cocoa Touch框架)进行互操作。在苹果平台上,Swift与Objective-C运行时库连接,使得C、C++、Objective-C和Swift代码能在同一个程序中运行。

版本历史[编辑]

版本 发布日期 macOS Linux Windows
Swift 1.0 2014年9月9日
Swift 1.1 2014年10月22日
Swift 1.2 2015年4月8日
Swift 2.0 2015年9月21日
Swift 2.1 2015年10月20日
Swift 2.2 2016年3月21日
Swift 2.2.1 2016年5月3日
Swift 3.0 2016年9月13日
Swift 3.0.1 2016年10月28日
Swift 3.0.2 2016年12月13日
Swift 3.1 2017年3月27日
Swift 3.1.1 2017年4月21日
Swift 4.0 2017年9月19日
Swift 4.0.2 2017年11月1日
Swift 4.0.3 2017年12月5日
Swift 4.1 2018年3月29日
Swift 4.1.1 2018年5月4日
Swift 4.1.2 2018年5月31日
Swift 4.1.3 2018年7月27日
Swift 4.2 2018年9月17日
Swift 4.2.1 2018年10月30日
Swift 4.2.2 2019年2月4日
Swift 4.2.3 2019年2月28日
Swift 4.2.4 2019年3月29日
Swift 5.0[24] 2019年3月25日
Swift 5.0.1 2019年4月18日
Swift 5.0.2 2019年7月15日
Swift 5.0.3 2019年8月30日
Swift 5.1 2019年9月10日
Swift 5.1.1 2019年10月11日
Swift 5.1.2 2019年11月7日
Swift 5.1.3 2019年12月13日
Swift 5.1.4 2020年1月31日
Swift 5.1.5 2020年3月9日
Swift 5.2 2020年3月24日
Swift 5.2.1 2020年3月30日
Swift 5.2.2 2020年4月15日
Swift 5.2.3 2020年4月29日
Swift 5.2.4 2020年5月20日
Swift 5.2.5 2020年8月5日
Swift 5.3 2020年9月16日 [25]
Swift 5.3.1 2020年11月13日
Swift 5.3.2 2020年12月15日
Swift 5.3.3 2021年1月25日
Swift 5.4[26] 2021年4月26日
Swift 5.4.1 2021年5月25日
Swift 5.4.2 2021年6月28日
Swift 5.4.3 2021年9月9日
Swift 5.5 2021年9月20日
Swift 5.5.1 2021年10月27日
Swift 5.5.2 2021年12月14日
Swift 5.5.3 2022年2月9日
Swift 5.6[27] 2022年3月14日
Swift 5.6.1[28] 2022年4月9日
Swift 5.6.2[29] 2022年6月15日
Swift 5.6.3[30] 2022年9月2日
Swift 5.7[31] 2022年9月12日
Swift 5.7.1[32] 2022年11月1日
Swift 5.8[33] 2023年3月30日
Swift 5.8.1[34] 2023年6月1日
Swift 5.9[35] 2023年9月18日

特色[编辑]

Swift取消了Objective-C的指针和其他不安全存取的使用,舍弃了Objective C早期套用Smalltalk风格的語法,全面改为句点表示法(dot-notation)。Swift具备类型推导(type inference)。同时,它提供了类似C++、C#中的命名空間(namespace)、泛型(generic)、运算符重载(operator overloading)。Swift被簡單的形容為 “沒有C的Objective-C”(Objective-C without the C)。[36]

類型和變量[编辑]

在Cocoa與Cocoa Touch的環境下,许多共用的类都放在Foundation Kit庫下,這些類別包含了NSString 字串庫(使用 Unicode),還有集合類別 NSArray 與 NSDictionary。Objective-C 提供了語法糖的方式讓這些物件可以整合在同一個語言裡。例如NSString之間合併的表示法如下:

NSString *str = @"hello,"; str = [str stringByAppendingString:@" world"]; 

在Swift裡, 字串的累積可以用加法(+)運算元作為頭等物件(First-class citizen)的方式直接完成, 上述的例子可以簡化為

var str="hello" str += " world" 

[37]

過去Cocoa(以及Cocoa Touch)的架構總是被分成兩種版本,一種是可變的(mutable),也就是可以在執行時期(runtime)改變;另一種是不可變的(immutable),其初始值不可改變,例如NSArray與NSMutableArray就是陣列的兩種版本。Swift語言延續這個傳統,但簡化為使用 let 關鍵字去設定常數(constant variable)。可变的(Mutable)对象使用 var 关键字,不可变的对象则适用let关键字。Swift语言使用類似C#Javascript的var定义变量,但特性又不盡相同。Swift不直接指派初始值給变量,变量无預設值,因此使用var变量之前必须先初始化,不然将发生編譯時期的錯誤。

为了方便使用,Swift提供Optional类型来宣告nil-free references。宣告Optional只需要在类型后面加上問號(?)即可。一旦var变量宣告为Optional的,其初始值就會被預設為nil。Optional本質上是enum,定义了None和Some两种类型,nil即是Optional.None。

函数库、执行时期与部署[编辑]

Swift在 Mac OS 和 iOS 平台可以和 Objective-C 使用相同的运行时(runtime),因此Swift 程序能运行在目前已存在的平台上,包括 iOS 6 和 OS X 10.8或更高的版本。[38] 更重要的, Swift 和 Objective-C 的代码可并存于同一程序代码, 这种延伸就如同 C 和 C++ 的关系一样。

为了吸引开发人员和复用已存在的代码,Xcode 6允许在 app target 导入 Objective-C 头文件供給 Swift 使用,通过 Objective-C 的桥接头文件(bridging header)来供给 Swift。当开发人员添加 Swift 文件到现有的 Objective-C应用程序时,Xcode自动建立相应的头文件。例如,一個 Swift 类 "MyClass" 可用于 Objective-C 的方式为

#import "MyClass-Swift.h".[39]

内存管理[编辑]

Swift使用自动引用计数(ARC)来管理内存。Apple以前要求在Objective-C中进行手动内存管理,但在2011年引入了ARC以简化内存管理。[40]在Objective-C中,强引用即为非ARC中的retain,而弱引用即为非ARC中的assign。不过,ARC的一个问题是可能创建一个强引用循环,即对象以一种可通过跟随引用来到达起始对象的方式互相引用(例如,A引用B,B引用A),这会导致它们不被释放而在内存中泄漏。Swift提供了关键字 weakunowned 来防止强引用循环。其中,weak 不能设定为可空类型,不能加上問號(?)或驚嘆號(!)。通常,父子关系会使用强引用,而子父关系会使用weak引用,其中父和子可以没有关联,或者使用unowned,其中子始终有父,但父可能没有子。弱引用必须是可选变量,因为它们可能会更改并变为nil[41]

类内的闭包也可以通过捕获self引用来创建强引用循环。可以使用capture list指示将self引用视为弱引用或无主引用。

除錯以及其他元素[编辑]

Xcode的除錯机制针对Swift语言提供REPL環境,可以利用Swift语法来evaluate或于其他程序互动,使Swift编程具有类似PythonRuby带有的交互性界面(Interactive Shell)等的特性而非传统的系统编程语言的交互特性。REPL进一步增强了Swift Playgrounds的功能,后者是在Xcode环境中运行的交互式视图,可以实时响应代码或调试器的变化。

类似 Objective-C之处[编辑]

  • 基本數值型別(numeric types)大致相同(如Int, UInt, Float, Double
  • 大量的C 语言运算符被移出Swift, 与此同时引入了新运算符。
  • 大括號被用於群組陳述(group statements)。
  • 變數之賦值使用等於符號, 但比較則使用“連續兩個等於”(==)運算元。還有一個新的運算元,“連續三個等於”(===)被用來判斷常數或變數之間是否為同一物件之實例(instance)。
  • 中括號([], Square brackets)用於陣列的表示, 宣告陣列之後, 可以指派索引值(index)來進行元素(element)之存取。
  • 控制陳述(control statement), for, while, if, switch 與Objective-C都十分類似, 但有延伸功能, 像是 for in 用於集合(collection)的輪詢,switch 還可以接受非整數的cases條件值, 諸如此類。

不同于 Objective-C之处[编辑]

  • 语句(statement)不需要用分号(;)結束,但分号可以作为一行上两个以上语句的分割符。
  • 不需要头文件(Header files)。
  • 注解方式 /* ... */ 嵌套,指注释里可以再有注释。
  • 強类型
  • 类型推论或隐含类型(Type inference)
  • 支持泛型編程
  • 函数成为第一等类型(first-class object),这意味着函数可以作为其他函数的参数和返回值。
  • 运算符重载。运算符可在类內重新定义。
  • 定义新的运算符。
  • 字符串全方面支持 Unicode
  • 规避了C語言家族的易错行为:
    • 不存在指针
    • 赋值语句没有返回值。若将if (i==0)誤寫成if (i=0),将造成编译時期錯誤。
    • switch 的区块内不需要使用 break 语句。另外,case后面都需要有可执行的代码(C或C++可連續使用多個case而不需要額外的代碼),否则将触发编译错误。
    • 变量和常量必须初始化才能使用,而且数组(array)的边界必须明确確認。
    • 溢位(overflows)的問題。C語言沒有強制整數型別(signed integers)的界限,常常在触发执行时期错误。Swift通过整数类型的max或min属性获取最大值或最小值。

讨论[编辑]

長達600多頁的 The Swift Programming Language 可以在iBooks免费下载。[42]

范例代码[编辑]

Swift不需要引入头文件(header file)或main()函数,不需要在每一行语句(statement)结尾加上分號。

变量声明方式如下:

var implicitInteger = 70 var implicitDouble = 70.0 var explicitDouble: Double = 70 

用 let 宣告常量方式如下:

let numberOfApples = 3 let numberOfOranges = 5 let appleSummary = "I have \(numberOfApples) apples." let fruitSummary = "I have \(numberOfApples + numberOfOranges) pieces of fruit." 

Swift不支持隐式类型转换(implicitly casting),因此数值和字符串之间必须显式类型转换(Explicitly casting):

let table = "The width of this table is " let width = 100 let tableWidth = table + String(width) 

以下是Hello World程式:

println("Hello, world") print("Hello, world") /*Swift 2.0 之後不再使用println,改使用print*/ 

使用Unicode字符,例如在文本中放心添加有不同语言的文字:

let  = ["新": 67, "青": 8, "玉": 33, "小小": 25] for (名字, ) in  {   print("\(名字)\() 歲。") } 

相关条目[编辑]

参考资料[编辑]

  1. ^ Swift Has Reached 1.0. Apple. September 9, 2014 [March 8, 2015]. (原始内容存档于2016-07-11). 
  2. ^ https://www.swift.org/blog/swift-5.10-released/; 出版日期: 2024年3月5日.
  3. ^ Swift, Objectively. [2022-04-09]. (原始内容存档于2022-01-08). Swift is proprietary and closed: It is entirely controlled by Apple and there is no open source implementation. 
  4. ^ Lattner, Chris. Re: [LLVMdev] [cfe-dev] [Advertisement] open positions in Apple's Swift compiler team. June 11, 2014 [June 12, 2014]. (原始内容存档于2014-07-14). You can imagine that many of us want it to be open source and part of LLVM, but the discussion hasn't happened yet, and won't for some time. 
  5. ^ 5.0 5.1 Lattner, Chris. Chris Lattner's Homepage. Chris Lattner. 2014-06-03 [2014-06-03]. (原始内容存档于2015-12-22). I started work on the Swift Programming Language in July of 2010. I implemented much of the basic language structure, with only a few people knowing of its existence. A few other (amazing) people started contributing in earnest late in 2011, and it became a major focus for the Apple Developer Tools group in July 2013 [...] drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.  引证错误:带有name属性“lattner 2014 info 1”的<ref>标签用不同内容定义了多次
  6. ^ Building assert() in Swift, Part 2: __FILE__ and __LINE__ - Swift B log -. Apple Developer. [2014-09-26]. (原始内容存档于2015-11-21). Swift borrows a clever feature from the D language: these identifiers expand to the location of the caller when evaluated in a default argument list. 
  7. ^ 7.0 7.1 Apple announces Swift, a new programming language for iOS. (原始内容存档于2015-12-26). 
  8. ^ 苹果新编程语言Swift仅耗时4年完成开发. 网易科技报道. 2014-06-05. (原始内容存档于2014-06-08). 
  9. ^ Swift 2.0 - Swift Blog. developer.apple.com. [2019-06-12]. (原始内容存档于2019-06-06) (美国英语). 
  10. ^ Swift 2 Apps in the App Store. Swift Blog. [2016-03-13]. 
  11. ^ Stack Overflow Developer Survey Results 2015. 
  12. ^ Stack Overflow Developer Survey Results 2016. 
  13. ^ The Swift Programming Language. Contribute to apple/swift development by creating an account on GitHub, Apple, 2019-06-12 [2019-06-12], (原始内容存档于2019-06-12) 
  14. ^ Inc, Apple. Swift.org. Swift.org. [2019-06-12]. (原始内容存档于2019-06-15) (英语). 
  15. ^ Swift.org - Download Swift. [2020-06-21]. 
  16. ^ Android SDKs for Swift. GitHub. [2021-09-10]. 
  17. ^ swift-lang package versions. [2021-09-10]. 
  18. ^ Swift Playgrounds. Apple Developer. [2016-06-19]. 
  19. ^ Swift Playgrounds — Preview. Apple. [2016-06-19] (美国英语). 
  20. ^ Mayo, Benjamin. Apple announces Swift Playgrounds for iPad at WWDC, public release in fall. 9to5Mac. 2016-06-13 [2016-06-19]. 
  21. ^ Cunningham, Andrew. Longtime Apple programmer and Swift creator leaves Apple for Tesla. Ars Technica. 2017-01-10. 
  22. ^ Wuerthele, Mike. New Swift project head Ted Kremenek said to be running the show behind the scenes for some time. AppleInsider. 2017-01-13. 
  23. ^ Daniel Eran Dilger. WWDC19: SwiftUI was the brightest star in a galaxy of new ideas. AppleInsider. [2019-07-19] (英语). 
  24. ^ Kremenek, Ted. Swift 5 Released!. Swift.org. 2019-03-25 [2019-03-28] (英语). 
  25. ^ Download Swift. Swift.org. Apple. [2020-12-15]. 
  26. ^ Kremenek, Ted. Swift 5.4 Released!. Swift.org. Apple. 2021-04-26 [2021-04-26]. (原始内容存档于2021-04-26) (英语). 
  27. ^ Kremenek, Ted. Swift 5.6 Released!. Swift.org. Apple. 2022-03-14 [2022-03-14] (英语). 
  28. ^ Release Swift 5.6.1 Release · apple/Swift. GitHub. 
  29. ^ Release Swift 5.6.2 Release · apple/Swift. GitHub. 
  30. ^ Release Swift 5.6.3 Release · apple/Swift. GitHub. 
  31. ^ Borla, Holly. Swift 5.7 Released!. Swift.org. Apple. 2022-09-12 [2022-09-23] (英语). 
  32. ^ Release Swift 5.7.1 Release · apple/Swift. GitHub. 
  33. ^ Release Swift 5.8 Release · apple/Swift. GitHub. 
  34. ^ Release Swift 5.8.1 Release · apple/swift. GitHub. [2023-06-14] (英语). 
  35. ^ Release Swift 5.9 Release · apple/swift. GitHub. [2023-09-18] (英语). 
  36. ^ Weber, Harrison. Apple announces ‘Swift,’ a new programming language for OS X & iOS. VentureBeat. 2014-06-02 [2014-06-10]. (原始内容存档于2016-11-17). 
  37. ^ "Strings and Characters"页面存档备份,存于互联网档案馆), Apple Inc.
  38. ^ "Do Swift-based apps work on OS X 10.9/iOS 7 and lower?"页面存档备份,存于互联网档案馆), StackOverflow
  39. ^ "Swift and Objective-C in the Same Project"页面存档备份,存于互联网档案馆), Apple Inc.
  40. ^ Documentation. docs.swift.org. [2023-07-08]. (原始内容存档于2023-09-20). 
  41. ^ Lanier, Brian; Groff, Joe. Intermediate Swift. Apple. [July 3, 2014]. (原始内容存档于2014-07-03). 
  42. ^ The Swift Programming Language页面存档备份,存于互联网档案馆).

外部链接[编辑]