Swift语言

蘋果公司開發的編程語言

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

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是为了替代苹果公司早期的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页面存档备份,存于互联网档案馆).

外部链接 编辑