Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Introduction to Swift

Introduction to Swift

Swift is a general-purpose programming language released by Apple in 2014, mainly used to develop apps for iOS, iPadOS, macOS, watchOS, and tvOS. After being open-sourced in 2015, it was handed over to the Swift community and Apple for joint maintenance; the source code of the compiler, standard library, and package manager can all be found on GitHub.

Swift combines the strengths of C and Objective-C while shedding the historical baggage of C compatibility. Compared with Objective-C, Swift has a more concise syntax and delegates error-prone areas such as null safety, type safety, and memory management to the compiler for compile-time checking.

Swift is a compiled language: source code is first compiled into machine code by the Swift compiler and then run directly, with no virtual machine required.

Swift supports four programming paradigms: procedural, object-oriented, functional, and protocol-oriented.

Note:Swift's syntax underwent a major revision in Swift 3; older online tutorials that still use println、++or C-style for loops are mostly written in the Swift 2 era and will no longer compile.


Swift Development History

Understanding version evolution helps you judge whether a piece of old code still works; the table below lists the key changes in each important version.

Year Version Milestone
2014 Swift 1.0 Released by Apple at WWDC, provided with Xcode 6
2015 Swift 2.0 Announced open source, added error handling, protocol extensions, and guard
2016 Swift 3.0 Major API naming changes, adjusted parameter label rules, removed C-style for loops
2017 Swift 4.0 Improved string APIs, introduced Codable
2019 Swift 5.0 ABI stability, binary frameworks can be reused across versions, introduced Result
2021 Swift 5.5 Introduced async/await (SE-0296), actors, and structured concurrency
2022 Swift 5.7 Introduced if let x { shorthand binding and the any existential type
2023 Swift 5.9 Introduced macros, package access level, and C++ interoperability
2024 Swift 6.0 Introduced strict concurrency checking, catching data races at compile time

Starting with Swift 5.5, the language entered a stable rhythm of "two minor releases per year", with ever smaller syntax changes and ever stronger engineering capabilities.

Swift 6 focuses not on new syntax but on moving concurrency safety from "possible runtime crashes" to "compile-time errors".


Why Choose Swift

Choosing a language usually comes down to four points: safety, performance, expressiveness, and ecosystem; Swift performs fairly well-balanced on all four.

Safety is Swift's most emphasized design goal.

The Optional type forces you to handle a possibly empty value before using it, eliminating crashes such as "null pointer dereference" at the language level.

Type safety prevents implicit conversion between different types; for example, Int and Double must be explicitly converted before arithmetic, so type errors are caught at compile time.

Integer overflow terminates the program by default instead of silently wrapping around, avoiding many hard-to-diagnose numeric bugs.

Speed is the second advantage.

The Swift compiler uses the LLVM backend and performs extensive inlining, generic specialization, and memory optimization, achieving performance close to C and far above scripting languages.

Fundamental types such as strings, arrays, and dictionaries are implemented as structs; combined with copy-on-write (COW), they offer both the safety of value semantics and the efficiency of reference semantics.

Expressiveness brings code closer to natural language.

Features such as string interpolation, closure shorthand, protocol extensions, and property wrappers let logic that takes dozens of lines in other languages be expressed in just a few lines in Swift.

Cross-platform support is a capability Swift gradually added after being open-sourced in 2015.

Swift can now compile and run on macOS, Linux, and Windows, and also supports server-side development and embedded devices.


What Can Swift Do

Swift's application scenarios are no longer limited to iPhone apps; the following sections describe what it can do by domain.

Domain Typical use Common Frameworks
Apple Platform Apps UI and logic for iOS, iPadOS, macOS, watchOS, and tvOS SwiftUI、UIKit、AppKit
Server-Side Development REST APIs, web services, microservices Vapor、Hummingbird
System and command-line tools Scripts, command-line programs, system services Foundation、SwiftPM
Machine learning Model inference and training Create ML、swift-coreml
Embedded Microcontrollers and resource-constrained devices Embedded Swift

If you are a beginner, start with Apple platform apps, since this is where Swift's toolchain is most mature and resources are most abundant.

If you are already familiar with backend development, you can use Vapor directly to write a web service, covering both frontend and backend with the same language.


Your First Swift Program

By convention, the first program starts by printing a line of text.

Example

// My first Swift program

print

(

"Hello, World!"

)

Save the code above as hello.swift, then run in the terminal:



$ swift hello.swift

Hello, World!

print is an output function provided by the Swift standard library; it automatically adds a newline at the end.

Unlike C, Swift requires no main function and no semicolons; top-level code in a file executes directly in order.

Let's look at a slightly more complete example that demonstrates type inference and string interpolation:

Example

import Foundation

// Type inference: the compiler automatically determines that site is a String

let site

=

"RUNOOB"

let year

=

2024

// String interpolation: use ( ) to embed values in a string

print

(

"Welcome to (site), current tutorial year (year)"

)

Running result:



欢迎来到 RUNOOB,当前教程年份 2024

let declares a constant and var declares a variable; the difference between them is explained in detail in Part 4.

Note:String interpolation is written inside double quotes, with the syntax of a backslash followed by parentheses \(expression), rather than writing it as in other languages {} or %s。