DEV Community

Cover image for Swift Programming: Variables, Control Flow, Functions, and Object-Oriented Concepts
Rohan Joshi
Rohan Joshi

Posted on • Edited on • Originally published at rohanjsh.Medium

Swift Programming: Variables, Control Flow, Functions, and Object-Oriented Concepts

Introduction to Swift Programming: From Basics

Before starting, Open Xcode, Click on File => New => Playground => iOS => Blank, and create a new playground to run Swift Code.

Swift Fundamentals

Variables and Constants

Swift uses var to declare variables and let to declare constants.

    var variableName: DataType = initialValue
    let constantName: DataType = initialValue
Enter fullscreen mode Exit fullscreen mode

Example:

    var greeting: String = "Hello"
    let pi: Double = 3.14159
Enter fullscreen mode Exit fullscreen mode

Constants (let) cannot change their value after they are set, while variables (var)may.

Data Types

Some common data types in Swift include:

  • String for text

  • Int for whole numbers

  • Bool for Boolean values

  • Double and Float for floating-point numbers

Swift is a type-safe language, meaning it encourages you to be clear about the types of values your code can work with.

Control Flow

Swift provides several ways to control the flow of code execution, including if statements, switch cases, and loops.

If Statement:

    if someCondition {
        // Code to execute if someCondition is true
    } else if anotherCondition {
        // Code to execute if anotherCondition is true
    } else {
        // Code to execute if the above conditions are false
    }
Enter fullscreen mode Exit fullscreen mode

Switch-cases

    switch <#some value to consider#> {
    case <#value 1#>:
        <#respond to value 1#>
    case <#value 2#>,
        <#value 3#>:
        <#respond to value 2 or 3#>
    default:
        <#otherwise, do something else#>
    }
Enter fullscreen mode Exit fullscreen mode

For-In Loop:

    for item in items {
        // Code to execute for each item in 'items'
    }
Enter fullscreen mode Exit fullscreen mode

While Loop:

    while someCondition {
        // Code to execute while someCondition remains true
    }
Enter fullscreen mode Exit fullscreen mode

Functions

Functions are self-contained chunks of code designed to carry out particular tasks.

Syntax:

    func functionName(parameterName: ParameterType) -> ReturnType {
        // Code
        return someValue
    }
Enter fullscreen mode Exit fullscreen mode

Example:

    func greet(person: String) -> String {
        let greeting = "Hello, \(person)!"
        return greeting
    }
Enter fullscreen mode Exit fullscreen mode

Here you have a function named greet that takes one parameter of type String and also returns a String.

Exercise: Try a simple function

Let’s write a function that takes a person’s name as a parameter and prints a customised greeting.

    func greet(person: String) {
        let greeting = "Starting iOS Development with, \(person)!"
        print(greeting)
    }

    greet(person: "Rohan") 
    // Outputs: Starting iOS Development with, Rohan!
Enter fullscreen mode Exit fullscreen mode

This function takes a String parameter and uses string interpolation to include it in a greeting, which is then printed to the console.

Classes and Structures

Swift uses classes and structures to define custom data types.

Structure Example:

    struct Person {
        var name: String
        var age: Int
    }

    var person1 = Person(name: "John", age: 25)
Enter fullscreen mode Exit fullscreen mode

Class Example:

    class Vehicle {
        var numberOfWheels: Int

    init(numberOfWheels: Int) {
            self.numberOfWheels = numberOfWheels
        }

    func information() {
            print("I have \(numberOfWheels) wheels")
        }
    }

    let myVehicle = Vehicle(numberOfWheels: 4)
    myVehicle.information() // Outputs: I have 4 wheels
Enter fullscreen mode Exit fullscreen mode

Classes are reference types, meaning that when you assign a class instance to a variable or constant, you are actually pointing to the same existing instance, whereas structures are value types, meaning that they replicate the instance. This is the primary distinction between classes and structures.

You now have a basic understanding of basic programming concepts and Swift syntax. Now write your function, experiment with conditionals and loops, and perhaps try defining a class or a structure. The logic in your programmes will be built using these technologies as the building blocks.

Practice. Experiment. Learn

Top comments (0)