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

Introduction to Swift Programming: From Basics — Part 2 of series

Rohan Joshi
3 min readJan 8, 2024

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

Example:

var greeting: String = "Hello"
let pi: Double = 3.14159

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
}

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#>
}

For-In Loop:

for item in items {
// Code to execute for each item in 'items'
}

While Loop:

while someCondition {
// Code to execute while someCondition remains true
}

Functions

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

Syntax:

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

Example:

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

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!

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)

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

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

--

--

No responses yet