Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Swift Syntax Tutorial

Introduction to Swift

Swift is a powerful and intuitive programming language created by Apple for building apps for iOS, Mac, Apple TV, and Apple Watch. Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love.

Variables and Constants

In Swift, you use var to declare variables and let to declare constants. A variable's value can change, while a constant's value cannot.

var myVariable = 42
myVariable = 50
let myConstant = 42

Data Types

Swift is a type-safe language, which means the language helps you be clear about the types of values your code can work with. The most common data types include:

  • Int for integer values
  • Double and Float for floating-point values
  • Bool for boolean values
  • String for textual data
  • Array for ordered collections
  • Dictionary for key-value associations
let integer: Int = 70
let double: Double = 70.0
let boolean: Bool = true
let string: String = "Hello, Swift!"

Control Flow

Swift provides all the traditional control flow structures such as loops and conditionals to help you write clean and efficient code.

If Statements

Use an if statement to conditionally execute a block of code:

let temperature = 30
if temperature > 25 {
    print("It's hot outside!")
} else {
    print("It's not so hot.")
}

For-In Loops

Use a for-in loop to iterate over a sequence, such as items in an array, ranges of numbers, or characters in a string:

let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
    print("Hello, \(name)!")
}

Functions

Functions are self-contained chunks of code that perform a specific task. You can define and call your own functions in Swift:

func greet(person: String) -> String {
    let greeting = "Hello, \(person)!"
    return greeting
}
print(greet(person: "Anna"))

Classes and Structures

Classes and structures are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your classes and structures by using the same syntax as for constants, variables, and functions.

class Person {
    var name: String
    init(name: String) {
        self.name = name
    }
    func greet() -> String {
        return "Hello, \(name)!"
    }
}
let person = Person(name: "John")
print(person.greet())

Optionals

Optionals are used in situations where a value may be absent. An optional either contains a value or contains nil to indicate that a value is missing.

var optionalString: String? = "Hello"
print(optionalString == nil)

var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
    greeting = "Hello, \(name)"
}

Enumerations

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

enum CompassPoint {
    case north
    case south
    case east
    case west
}
var direction = CompassPoint.north
direction = .east