コンテンツにスキップ

Swift (プログラミング言語)

出典: フリー百科事典『ウィキペディア(Wikipedia)』
Swift
Swift
Swiftのロゴ
パラダイム 関数型プログラミング命令型プログラミングオブジェクト指向プログラミング、マルチパラダイムプログラミング、block-structured programming、宣言型プログラミング ウィキデータを編集
登場時期 2014年6月2日 (2014-06-02)
開発者 クリス・ラトナー、Apple ウィキデータを編集
最新リリース 5.10.1 / 2024年6月5日[1]
型付け 強い静的型付け型推論
影響を受けた言語 RustHaskellRubyObjective-CPythonC SharpCLUC++JavaScalaSmalltalkGroovyD言語 ウィキデータを編集
プラットフォーム macOSiOSLinuxMicrosoft Windows 10iPadOStvOSwatchOSMicrosoft WindowsvisionOS ウィキデータを編集
ライセンス Apache-2.0、Apache License ウィキデータを編集
ウェブサイト
拡張子 swift ウィキデータを編集
テンプレートを表示

SwiftAppleiOSmacOSLinuxWindowsWorldwide Developers Conference (WWDC) 2014AppleOSObjective-CObjective-C++CObjective-C使[2]

SwiftObjective-CObjective-C[3] 

SwiftXcodePlaygrounds

LLVM使[4]

[]


Swift2010LLVMClangChris LattnerChrisSwiftObjective-CRustHaskellRubyPythonC#CLU[5]

Apple42014WWDCApple

201499SwiftXcode 6.01.0[6]

2015123SwiftApache 2.0 Swift.org[ 1] 

WWDC 2016調AppleSwiftSwift PlaygroundsiPad20169[7]3D2017321Swift 3.15[8]Swift 3.12017327[9]

2017Project LeadChris LattnerLLVMClangAppleTed Kremenek[10]

2017919Xcode 9.0Swift 4[11]Swift 3Xcode使2018329Xcode 9.3Swift 4.1[12]201864Xcode 10 betaSwift 4.2[13]

2019325Xcode 10.2Swift 5AppleOSSwift[14]Swift 4.x

WWDC 2019AppleSwiftUI[15]

2019920Xcode 11.0Swift 5.1Swift[16]

2020324Xcode 11.4Swift 5.2[17]

2020916Swift 5.3[18]1922Windows[19]

2021WWDCApple Swift 5.5 Async/await [20]

202435Swift 5.10[21]Swift 6

[]


AppleSwift[22][2]



Optional




ifswitch-case



Swift (REPL) 
SwiftXcode6Playgrounds

SwiftCObjective-C[2]

サンプルコード[編集]

Hello World[編集]

print( "Hello, World!" )  // これだけで動いて、Hello, World! と出力される。

[編集]

/* 
 * コメントはCスタイルの複数行コメントと…
 */
// C++スタイルの一行コメントの双方をサポートしている

// var name: Type = value でType型の変数nameを宣言し、valueで初期化する
var explicitDouble: Double = 70 // 70.0
 
/// 型が省略された場合は、型推論により初期値の型が適用される
var implicitInteger = 70    // Int
var implicitDouble = 70.0   // Double
 
// let name:Type = value でType型の定数nameにvalueを設定する。
// 型推論可能な場合、型の表記は省略できる。
let theAnswer = 42

// 識別子にはたいていのUnicode文字を用いることができる。
let リンゴの数 = 3
let みかんの数 = 5

// 文字列リテラル"..."の中にある\(expr)には、式exprの内容が展開される
let リンゴ説明 = "私は\(リンゴの数)個のリンゴを持っている。"  // ”私は3個のリンゴを持っている。"
let 果物説明 = "私は\(リンゴの数 + みかんの数)個の果物を持っている。" //"私は8個の果物を持っている。"

// Swiftでは辞書も組み込みでサポートされている。
// 以下は Dictionary<String, Int> 型の定数辞書の定義の一例である。
let people = ["Anna": 67, "Bety": 8, "Jack": 33, "Sam": 25]

// 辞書の内容の列挙は for (key, value) in dict { ... }
for (name, age) in people {
    print("\(name)is\(age) years old.")
}

// メソッドや関数は "func"文法を使って宣言する。
// パラメータ名の付け方に注意。-> で戻り値の型を宣言する
func sayHello(to personName: String) -> String {
    let greeting = "こんにちは、" + personName + "さん"
    return greeting
}
// "こんにちは、サーバーさん"を出力
print(sayHello(to: "サーバー"))

メモリ管理 (Automatic Reference Counting)[編集]


Swift (Automatic Reference Counting) ARC使ARC使使Objective-C使releaseretain

Swiftweakunowned使[23]

相互運用性[編集]

SwiftはCocoaObjective-Cをシームレスに使えるように設計されている[24]。SwiftとObjective-Cの間はどちらのAPIからでもお互いに使う事が出来る。

モジュールとしてアクセス可能などんなObjective-Cフレームワーク(又はCライブラリ)でもSwiftに直接インポートできる。

import Foundation

初期化の例 Objective-C

UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

Swift

let myTableView: UITableView = UITableView(frame: .zero, style: .Grouped)

Xcode 6Objective-CSwiftSwiftObjective-CSwift[25]Objective-CSwift[26]SwiftObjective-C[27]

C言語APIとの互換[編集]

SwiftからLibcを呼ぶ例

puts("Hello from libc")
let fd = open("/tmp/scratch.txt", O_WRONLY|O_CREAT, 0o666)

if fd < 0 {
    perror("could not open /tmp/scratch.txt")
} else {
    let text = "Hello World"
    write(fd, text, strlen(text))
    close(fd)
}

その他の実装、開発[編集]


Swift 2.2UbuntuSwift 5.2.4CentOSAmazon Linux[28]

Swift[29]WebIBMKituraVaporAppleServer APIsSwift[30]

Android[31]

2020529AWS LambdaSwiftSwift AWS Lambda Runtime[32]

注釈[編集]

出典[編集]



(一)^ URL: https://github.com/apple/swift/releases/tag/swift-5.10.1-RELEASE, : 202465

(二)^ abcWilliams, Owen (201462). Apple announces Swift, a new programming language for iOS. The Next Web. 201462

(三)^ Protocol-oriented Programming in Swift. Apple Inc. YouTube.

(四)^ Introducing Swift.  Apple. 201462

(五)^ Chris Lattner's Homepage (2020624)

(六)^ Swift Has Reached 1.0 - Swift Blog - Apple Developer

(七)^ Swift PlaygroundsApp Store

(八)^ Swift Playgrounds5

(九)^ Swift 3.1 Released! - swoft.org (2017327)

(十)^ [swift-evolution Update on the Swift Project Lead] - Chris Lattner (2017110)

(11)^ Swift 4.0 Released! - Ted Kremenek (2017919)

(12)^ XcodeMac App Store. Mac App Store. 201842

(13)^ Xcode 10 includes Swift 4.2, which compiles your software more quickly, helps you deliver faster apps, and generates even smaller binaries. Xcode 10 (). Developer. 2018612

(14)^ Swift.org - Swift 5 Released! (). 2020318

(15)^ Introducing SwiftUI: Building Your First App - WWDC 2019 - Videos - Apple Developer (). 2020323

(16)^ Swift.org - Swift 5.1 Released! (). 2020112

(17)^ Swift.org - Swift 5.2 Released! (). 20201125

(18)^ Swift.org - Swift 5.3 released! (). 2021413

(19)^ Introducing Swift on Windows (). 2020112

(20)^ Hudson, Paul (202166). What's new in Swift 5.5?. HackingWithSwift.com.  Hacking with Swift. 2021729

(21)^ Swift.org - Swift 5.10 Released (). 2024411

(22)^ kojima (197011). Swift. xhours - . 20231228

(23)^ Automatic Reference Counting  The Swift Programming Language (Swift 5.2) (). 2020525

(24)^ Using Swift with Cocoa and Objective-C (HTML).  Apple Inc.. 201492

(25)^ Importing Objective-C into Swift.  Apple Inc.. 2020622

(26)^ Importing Swift into Objective-C.  Apple Inc.. 2020622

(27)^ Migrating Your Objective-C Code to Swift.  Apple Inc.. 2020622

(28)^ Swift.org - Download Swift. 2020112

(29)^ IBM brings Swift to the cloud, releases web framework Kitura written in Apples programming language. 2020323

(30)^ Inc., Apple. Server APIs Work Group. Swift.org. https://swift.org/blog/server-api-workgroup/ 2020525 

(31)^ Swift for Android: Our Experience and Tools. 2021324

(32)^ Swift.org - Introducing Swift AWS Lambda Runtime. https://swift.org/blog/aws-lambda-runtime/ 2020924 

外部リンク[編集]