Differences Between Static & Instance Functions in Swift
Written by Team Kodeco
There are multiple fundemental differences between static and instance functions or properties in Swift:
-
Scope: Static functions and properties belong to the type itself, rather than to instances of the type. They can be called or accessed without creating an instance of the type. Instance functions and properties belong to instances of the type, and they must be called or accessed on an instance of the type.
-
Syntax: Static functions and properties are declared using the static keyword in front of the function or property definition. Instance functions and properties are declared without the static keyword.
-
Functionality: Static functions and properties can be used to encapsulate logic that is not dependent on individual instances of the type, and that is shared by all instances of the type. Instance functions and properties can be used to encapsulate logic that is dependent on individual instances of the type, and that is specific to each instance.
-
Accessibility: Static functions and properties can be accessed from anywhere in the program, regardless of the context or the visibility of the type. Instance functions and properties can only be accessed from within the instance of the type, or from within a method of the type that operates on instances of the type.
-
Memory: Static functions and properties are stored in a single location in memory, and are shared by all instances of the type. Instance functions and properties are stored in separate locations in memory, one for each instance of the type.
Here’s an example to demonstrate the differences between static and instance functions and properties in Swift:
class Car {
static var manufacturer = "Unknown"
var color = "Unknown"
static func describeManufacturer() -> String {
return "The manufacturer is \(manufacturer)."
}
func describeColor() -> String {
return "The color is \(color)."
}
}
let redCar = Car()
redCar.color = "Red"
let greenCar = Car()
greenCar.color = "Green"
Car.manufacturer = "Toyota"
print(Car.describeManufacturer()) // "The manufacturer is Toyota."
print(redCar.describeColor()) // "The color is Red."
print(greenCar.describeColor()) // "The color is Green."
This code defines a class Car
with two properties, manufacturer
and color
, and two methods, describeManufacturer()
and describeColor()
.
manufacturer
is a static property with a default value of “Unknown”. color
is an instance property with a default value of “Unknown”.
describeManufacturer()
is a static function that returns a string describing the manufacturer of the car. describeColor()
is an instance function that returns a string describing the color of the car.
The code then creates two instances of Car
, redCar
and greenCar
, and sets their color properties to “Red” and “Green” respectively.
The code sets the manufacturer property of Car
to “Toyota” and then prints the results of calling describeManufacturer()
and describeColor()
on Car
and on both instances, redCar
and greenCar
.