Getting Started
Swift Introduction
Get started with Swift — Apple's powerful, safe, and expressive language for iOS and macOS.
What is Swift?
Swift is a general-purpose programming language developed by Apple. It is the primary language for developing apps for iOS, macOS, watchOS, and tvOS. Swift is also available on Linux and Windows.
Key Features
- Safe: Designed to prevent common programming errors (null safety, type safety)
- Fast: Performance comparable to C
- Modern: Clean syntax with closures, generics, tuples
- Expressive: Protocol-oriented programming, property wrappers
- Open source: Available at swift.org
Swift vs Objective-C
Swift replaced Objective-C as Apple's primary language. Swift is:
- Easier to read and write
- Safer (optional types prevent null crashes)
- More expressive
- Interoperable with Objective-C
Getting Started
On macOS: install Xcode from the App Store. You can also use Swift Playgrounds or online playgrounds at swift.apple.com.
Example
swift
// Hello World in Swift
print("Hello, Swift!")
// Constants and variables
let name = "Swift" // let = constant (immutable)
var version = 5.9 // var = variable (mutable)
// name = "Other" // Error! Constants can't change
version = 6.0 // Fine
// Type annotations
let city: String = "London"
var count: Int = 0
var price: Double = 9.99
var isActive: Bool = true
// Type inference (Swift infers the type)
let language = "Swift" // inferred as String
let pi = 3.14159 // inferred as Double
// String interpolation
print("Hello, \(name) \(version)!")
print("Pi is approximately \(pi)")
// Multi-line strings
let poem = """
Roses are red,
Violets are blue,
Swift is awesome,
And so are you!
"""
print(poem)Try it yourself — SWIFT