Swift
In short
Swift is a compiled, statically typed language designed for safety and speed, used mainly to build apps for iPhone, iPad, Mac, and other Apple platforms.
What is the Swift programming language?
Swift is a general-purpose programming language introduced by Apple in 2014 as a modern successor to Objective-C and released as open source in 2015. It is the main language for building apps on iPhone, iPad, Mac, Apple Watch, and other Apple platforms, and it also runs on Linux and Windows. Swift aims to be safe by default, fast, and expressive, with a clean syntax that avoids much of C's punctuation.
Swift is statically typed with type inference. A value that might be missing has an optional type, such as Int?, and must be safely unwrapped, for example with if let, before it can be used, which prevents many crashes caused by unexpected nil values. Swift uses value types heavily: struct and enum values are copied when assigned, while class instances are shared by reference. Since Swift 6, the compiler can also check for data races between concurrent tasks at compile time.
Memory is managed with Automatic Reference Counting (ARC). Each class instance keeps a count of how many references point to it, and it is freed the moment that count drops to zero, much like a meeting room that stays booked while anyone is inside and becomes free as soon as the last person leaves. ARC cannot clean up reference cycles, where two objects refer to each other, so developers mark one side as weak to avoid memory leaks.
ARC is often confused with garbage collection. A tracing garbage collector, as used in Java, Go, or C#, periodically scans memory to find unused objects, while ARC updates counts as references are added and removed and frees objects deterministically. Swift is also often compared with Kotlin: both are modern languages with null safety and type inference, but Swift mainly targets Apple platforms while Kotlin is centered on the JVM and Android.
Key takeaways
- Swift is the main language for building apps on Apple platforms.
- It is statically typed, with optionals that make missing values explicit.
- Memory is managed with Automatic Reference Counting (ARC), not a tracing garbage collector.
- Structs and enums are value types, which are copied instead of shared.
- Swift is open source and also runs on Linux and Windows.
Example
struct Book {
let title: String
let rating: Int? // Optional: may be nil
}
let books = [Book(title: "Dune", rating: 5), Book(title: "Emma", rating: nil)]
for book in books {
// if let safely unwraps the optional before using it
if let rating = book.rating {
print("\(book.title): \(rating) stars")
} else {
print("\(book.title): not rated yet")
}
}Readers ask
Is Swift only for Apple devices?
Swift is used mostly for Apple platforms, but the language is open source and also runs on Linux and Windows, where it is used for server-side code and command-line tools.
What is the difference between Swift and Objective-C?
Objective-C is the older language for Apple platforms, built on top of C with a message-passing syntax. Swift is newer, safer, and more concise, and the two can be used together in the same project.
Does Swift use garbage collection?
No. Swift uses Automatic Reference Counting (ARC), which frees an object as soon as nothing refers to it. Developers must break reference cycles themselves, usually with weak or unowned references.
See also
- KotlinProgramming Languages, p. 18Kotlin is a concise, statically typed language that runs on the JVM, works seamlessly with Java, and is widely used for Android and server-side development.
- Memory LeakOperating Systems, p. 19A memory leak is a bug where a program keeps holding memory it no longer needs, so its memory usage grows over time and can slow down or crash the system.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- Type InferenceProgramming Fundamentals, p. 55Type inference is a compiler feature that works out the type of a variable or expression automatically, so you don't have to write every type annotation.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- ConcurrencyProgramming Fundamentals, p. 12Concurrency is a program's ability to make progress on several tasks in overlapping time periods, such as serving many users at once rather than one at a time.
- DartProgramming Languages, p. 8Dart is a statically typed, garbage-collected language from Google that compiles to native code and JavaScript, best known as the language of Flutter apps.
- Objective-CProgramming Languages, p. 21Objective-C is an object-oriented superset of C with Smalltalk-style messaging, and it was the main language for macOS and iOS apps before Swift arrived.
Spotted a mistake or something missing on this page?Suggest an edit