Introduction
Swift is a general-purpose, compiled programming language created by Apple to build applications for iOS, macOS, watchOS, and tvOS. It was designed to be a safer, faster, and more expressive replacement for Objective-C, while remaining fully compatible with Apple's existing Cocoa and Cocoa Touch frameworks. Swift combines modern language features such as type inference and closures with the performance of a compiled, statically typed language.
Cricket analogy: Swift replacing Objective-C for Apple platforms is like a franchise switching from an aging all-rounder to a faster, safer replacement while keeping compatibility with the existing squad structure, or Cocoa frameworks, already in place.
Syntax
// A basic Swift program
var greeting = "Hello"
let name = "World"
print("\(greeting), \(name)!")Explanation
In this snippet, 'var' declares a mutable variable while 'let' declares an immutable constant. Swift infers the type of both 'greeting' and 'name' as String automatically, so no explicit type annotation is required. The '\()' syntax is called string interpolation, and it lets you embed the values of variables directly inside a string literal. The 'print' function then writes the resulting string to the console.
Cricket analogy: 'var runsScored' declares a mutable tally that updates ball by ball, while 'let playerName' is a fixed constant like Kohli, and string interpolation '\()' embeds runsScored directly into a commentary string that print() sends to the broadcast.
Example
func describeLanguage(_ language: String, fast: Bool) -> String {
if fast {
return "\(language) is designed to be fast and safe."
} else {
return "\(language) is a programming language."
}
}
let description = describeLanguage("Swift", fast: true)
print(description)Output
Swift is designed to be fast and safe.Key Takeaways
- Swift is Apple's modern language for iOS, macOS, watchOS, and tvOS development.
- It was designed as a safer, faster alternative to Objective-C.
- Swift uses type inference, so explicit type annotations are often optional.
- String interpolation with \() makes building strings from variables easy.
- Swift is a compiled, statically typed language, giving it strong performance.
Practice what you learned
1. Which company created the Swift programming language?
2. What keyword is used to declare an immutable value in Swift?
3. What is the primary purpose Swift was originally designed for?
4. How do you embed a variable's value inside a string literal in Swift?
5. Which statement best describes Swift's type system?
Was this page helpful?
You May Also Like
History and Evolution of Swift
Swift evolved from a 2014 Apple announcement into an open-source, ABI-stable language used across and beyond Apple platforms.
Features of Swift
Swift combines safety, speed, and modern syntax through optionals, type inference, ARC, and a powerful LLVM-based compiler.
Setting Up a Swift Environment
Learn how to install Xcode, Swift Playgrounds, the REPL, Swift Package Manager, and Linux toolchains to start coding in Swift.
Variables and Constants in Swift
Learn how Swift's var and let keywords declare mutable variables and immutable constants, and why let is preferred by default.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics