Introduction to Swift
## Learning Objectives
- Understand Swift history and evolution
- Set up Swift development environment
- Write and run your first Swift program
- Understand Swift syntax basics
## What is Swift?
Swift is a powerful, intuitive programming language developed by Apple for building apps for iOS, Mac, Apple TV, and Apple Watch. It was designed to be easy to learn and use while being expressive and modern.
```swift
print("Hello, World!")
```
## Swift History
| Year | Version | Milestone |
|------|---------|-----------|
| 2014 | Swift 1.0 | Initial release |
| 2015 | Swift 2.0 | Open source, protocol extensions |
| 2016 | Swift 3.0 | API design guidelines, Linux support |
| 2017 | Swift 4.0 | Improved generics, string handling |
| 2019 | Swift 5.0 | ABI stability, SwiftUI announced |
| 2023 | Swift 5.9 | Macro support, if/switch expressions |
## Key Features
- **Safe**: Built-in memory safety and type safety
- **Fast**: Performance-optimized compiled language
- **Modern**: Concise syntax, optionals, closures
- **Interactive**: Playgrounds for experimentation
- **Open Source**: Available on GitHub
## Installing Swift
### macOS
Swift comes pre-installed with Xcode. To verify:
```bash
swift --version
```
### Linux
Download from :
```bash
# Ubuntu (example)
wget https://download.swift.org/swift-5.9.2-release/ubuntu2204/swift-5.9.2-RELEASE-swift-5.9.2-RELEASE-ubuntu22.4.tar.gz
tar xzf swift-5.9.2-RELEASE-*
export PATH=/path/to/swift-5.9.2-RELEASE-ubuntu22.4/usr/bin:"$PATH"
```
### Verify Installation
```bash
swift --version
# Swift version 5.9.2
```
## Your First Program
### Hello World
```swift
print("Hello, World!")
```
### Using Variables
```swift
let name = "Swift"
let version = 5.9
print("Welcome to \(name) \(version)")
```
## Swift Playgrounds
Playgrounds provide an interactive environment to write and test Swift code.
### Creating a Playground (Xcode)
1. Open Xcode
2. File > New > Playground
3. Select iOS or macOS
4. Name your playground
5. Start coding!
### Playground Benefits
- Instant feedback
- Experiment freely
- Visualize data
- Test algorithms
## Swift Program Structure
### Basic Structure
```swift
// Entry point
import Foundation
let message = "Hello"
print(message)
```
### Multiple Statements
```swift
let a = 10
let b = 20
let sum = a + b
print("Sum: \(sum)")
```
## Comments
```swift
// Single-line comment
/*
Multi-line comment
*/
/**
Documentation comment (used by Xcode)
*/
```
## Data Types Preview
```swift
// Integers
let age: Int = 25
// Floating-point
let price: Double = 19.99
// Strings
let name: String = "Swift"
// Booleans
let isActive: Bool = true
// Arrays
let numbers: [Int] = [1, 2, 3]
// Dictionaries
let person: [String: Any] = ["name": "Alice", "age": 30]
```
## Command Line
### Running Swift Files
```bash
swift MyFile.swift
```
### Compiling
```bash
swiftc -o MyProgram MyFile.swift
./MyProgram
```
## IDEs
### Xcode (Recommended)
- Native Apple IDE
- Built-in playgrounds
- Interface Builder
- Debugger
### VS Code
- Install Swift extension
- Syntax highlighting
- IntelliSense
### Swift Playgrounds App
- iPad app
- Interactive coding
- Great for learning
## Swift vs Other Languages
| Feature | Swift | Objective-C | Java |
|---------|-------|-------------|------|
| Type Safety | Strong | Weak | Strong |
| Memory Safety | Yes | Manual | Yes |
| Syntax | Modern | C-based | C-based |
| Optionals | Built-in | Nil objects | No |
| Closures | Native | Blocks | Lambdas |
## Summary
- Swift is Apple's modern programming language
- Available on macOS, Linux, and Windows
- Swift Playgrounds enable interactive learning
- Type-safe and memory-safe by default
- Run with `swift filename.swift`
- Comments: `//`, `/* */`, `/** */`
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →