한국   대만   중국   일본 
Swift語言 - ?基百科,自由的百科全? 跳?到?容

Swift語言

本页使用了标题或全文手工转换
?基百科,自由的百科全?
Swift
?程范型 範式 物件導向 泛函 指令式 組塊 英? Block_(programming) 結構化
設計者 克里斯·拉特納 ?果公司
實作者 ?果公司
?行?? 2014年6月2日 ,​10年前 ​( 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-C Rust Haskell Ruby Python C# CLU [5] ,和 D語言 [6]

Swift編程語言, 支持 多?程范式 編譯 式,用?撰?基于 macOS/OS X iOS iPadOS watchOS tvOS visionOS 的?件 [7] 。 ?果公司于2014年在 ?果??者年? (WWDC)發布了Swift?程?言。???上 ?果公司 ?Swift? Objective-C 共存在?果公司的 操作系? [7]

歷史 [ ?? ]

Swift是?了替代?果公司早期的Objective-C?程?言,??言自20世?80年代初以?基本?有任何更新,缺乏?代?言功能。

2010年7月,?果??者工具部??? 克里斯·拉特納 ?始着手 Swift ?程?言的設計工作,以一年時間,完成基本架構後,他領導了一個設計團隊參與其中,??果的?多程序?共同??,借?了 Objective-C Rust Haskell Ruby Python C# CLU 等?多其他?言的設計思想。 [5] 2014年6月2日,?果在 WWDC 上?布了第一?公?的使用Swift??的?用程序:WWDC?用程序,同?向?果的注???者?布了Swift?言的??版,但未承?最?版本???版本的源代?相互兼容。大?也?布了??500?的官方指南, The Swift Programming Language ,在 Apple Books 和官方?站上免?提供。?果??在完整版?布?,如果有必要,??提供源代???器。

?果宣?Swift的特点是:快速、?代、安全、互?,而且明顯優於Objective-C?言。Swift以 LLVM 編譯,可以使用?有的 Cocoa Cocoa 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日,?果於 WWDC 2015上?布了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起,包括 CentOS Amazon 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 / macOS iOS iPadOS tvOS watchOS visionOS )和 Linux Windows Android 系?。

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提供了??字 weak unowned ?防止强引用循?。其中, weak 不能?定?可空?型,不能加上問號(?)或驚嘆號(!)。通常,父子?系?使用强引用,而子父?系?使用 weak 引用,其中父和子可以?有??,或者使用 unowned ,其中子始?有父,但父可能?有子。弱引用必?是可??量,因???可能?更改??? nil [41]

??的?包也可以通?捕?self引用??建强引用循?。可以使用capture list指示?self引用??弱引用或无主引用。

除錯以及其他元素 [ ?? ]

Xcode的除錯机制??Swift?言提供 REPL 環境,可以利用Swift?法?evaluate或于其他程序互?,使Swift?程具有?似 Python Ruby ?有的交互性界面(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 ] . (原始?容 存? 于2016-08-26).  
  11. ^ Stack Overflow Developer Survey Results 2015 . [ 2023-11-25 ] . (原始?容 存? 于2016-12-31).  
  12. ^ Stack Overflow Developer Survey Results 2016 . [ 2023-11-25 ] . (原始?容 存? 于2017-02-20).  
  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 ] . (原始?容 存? 于2016-07-08).  
  16. ^ Android SDKs for Swift . GitHub . [ 2021-09-10 ] . (原始?容 存? 于2022-10-23).  
  17. ^ swift-lang package versions . [ 2021-09-10 ] . (原始?容 存? 于2024-03-24).  
  18. ^ Swift Playgrounds . Apple Developer. [ 2016-06-19 ] . (原始?容 存? 于2017-07-01).  
  19. ^ Swift Playgrounds ? Preview . Apple. [ 2016-06-19 ] . (原始?容 存? 于2022-07-08) (美?英?) .  
  20. ^ Mayo, Benjamin. Apple announces Swift Playgrounds for iPad at WWDC, public release in fall . 9to5Mac. 2016-06-13 [ 2016-06-19 ] . (原始?容 存? 于2022-06-30).  
  21. ^ Cunningham, Andrew. Longtime Apple programmer and Swift creator leaves Apple for Tesla . Ars Technica. 2017-01-10 [ 2023-11-25 ] . (原始?容 存? 于2017-02-18).  
  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 [ 2023-11-25 ] . (原始?容 存? 于2024-04-16).  
  23. ^ Daniel Eran Dilger. WWDC19: SwiftUI was the brightest star in a galaxy of new ideas . AppleInsider. [ 2019-07-19 ] . (原始?容 存? 于2024-03-08) (英?) .  
  24. ^ Kremenek, Ted. Swift 5 Released! . Swift.org. 2019-03-25 [ 2019-03-28 ] . (原始?容 存? 于2023-06-18) (英?) .  
  25. ^ Download Swift . Swift.org. Apple. [ 2020-12-15 ] . (原始?容 存? 于2016-07-08).  
  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 ] . (原始?容 存? 于2023-07-23) (英?) .  
  28. ^ Release Swift 5.6.1 Release · apple/Swift . GitHub . [ 2023-11-25 ] . (原始?容 存? 于2022-04-10).  
  29. ^ Release Swift 5.6.2 Release · apple/Swift . GitHub . [ 2023-11-25 ] . (原始?容 存? 于2024-04-18).  
  30. ^ Release Swift 5.6.3 Release · apple/Swift . GitHub . [ 2023-11-25 ] . (原始?容 存? 于2024-02-04).  
  31. ^ Borla, Holly. Swift 5.7 Released! . Swift.org. Apple. 2022-09-12 [ 2022-09-23 ] . (原始?容 存? 于2024-04-03) (英?) .  
  32. ^ Release Swift 5.7.1 Release · apple/Swift . GitHub . [ 2023-11-25 ] . (原始?容 存? 于2024-02-04).  
  33. ^ Release Swift 5.8 Release · apple/Swift . GitHub . [ 2023-11-25 ] . (原始?容 存? 于2024-02-04).  
  34. ^ Release Swift 5.8.1 Release · apple/swift . GitHub. [ 2023-06-14 ] . (原始?容 存? 于2024-02-04) (英?) .  
  35. ^ Release Swift 5.9 Release · apple/swift . GitHub. [ 2023-09-18 ] . (原始?容 存? 于2024-02-04) (英?) .  
  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 ?面存??? ,存于 互???案? ).

外部?接 [ ?? ]