Introduction
A .swift file holds source code in Swift,
the compiled language Apple announced in 2014 and released as open source in 2015. It is
the main language for iPhone, iPad and Mac apps, and also runs on Linux and Windows.
All the .swift files of a module are compiled together, so one file can use
what another declares without importing it. A Package.swift file, itself
written in Swift, describes a project to the Swift Package Manager.
Structure
A Swift file is UTF-8 text with no header. It usually opens with import
lines, then declarations in any order: struct, class,
enum, protocol, extension and func.
Constants are declared with let and variables with var.
Semicolons are optional, and a line break normally ends a statement. Comments are
// to the end of the line and /* */, which can nest.
Statements at the top level of a file are only allowed in a file called
main.swift or in a single file run as a script. Otherwise the program starts
at a type marked @main.
import Foundation
struct Greeter {
let name: String
func greeting() -> String {
return "Hello, \(name)"
}
}
@main
struct App {
static func main() {
print(Greeter(name: "world").greeting())
}
}
@main entry point.Not described
Known, not yet covered
- Reference: this page is the only one for Swift, and it is an overview. These parts of the language have no reference pages: lexical structure, types, expressions, statements, declarations, functions and closures, structures, classes and enumerations, protocols and extensions, generics, patterns, attributes, error handling, concurrency, macros, memory and ownership, modules and access control, operators and the language versions. The Swift Programming Language describes Swift 6.4.
- Reference: the standard library (
Array,Dictionary,String,Result, the concurrency types), the Apple frameworks (Foundation, SwiftUI) and thePackage.swiftmanifest API are not described, and neither are the compiler flags or the interface files that a built module carries. - Viewer: a string is read with its
\( … )parts, which may hold strings of their own ("a \("b \(1)")"is one string, to 32 levels), and a raw string ends only at a quote followed by as many#as it opened with and interpolates only with\#( … )(#"raw \(x) "q""#and##"a"#b"##are each one string). The contextual wordsmacro(before a name and(or<),borrowing,consumingandisolated(before a type or name) are keywords only in that position. A regex literal (/ab+c/) and a key path (\Foo.bar) are plain, and a#macro call such as#externalMacrois coloured as a function. - Viewer: only the standard type names (
Int,String,Array,Dictionaryand the like) are coloured as types. A type you declare, a generic parameter or a framework type (View) is plain, and a call is coloured as a function by name only. The\( … )parts of a string are part of the string colour. - Viewer: the file is not parsed or compiled, so there is no outline, no type checking and no list of problems, and only the first 1 MiB is read and coloured.
Links
Software
- Swift – the official compiler and package manager (macOS, Linux, Windows)
- Xcode – Apple’s development environment (macOS)
- Visual Studio Code – free code editor with a Swift extension (Windows, macOS, Linux)
Developers
- swift-syntax – libraries for parsing, inspecting and generating Swift source (Swift)
- SourceKit-LSP – language server that gives editors Swift completion and navigation
- tree-sitter-swift – Swift grammar for the Tree-sitter parser used by editors
Reference
- The Swift Programming Language – the official book, with the language reference