Swift — Struct vs Class

Irina Ernst
2 min readSep 25, 2016

--

Structures are value types. When we passing structures from one function to another, a new instance of them is created and then passed to the function.

Person.swift
AppDelegate.swift
AppDelegate.swift

=> The value of the name and age property of the person instance is changed only in the context of the function, bit not outside of the function! The instance of the structure Person was passed to the function to change the name and the age to a given string/integer. The structure Person was copied into the stack and passed to the function. The mutating function is not changeing the name and the age of the ann variable.

Classes are reference types. They are passed around to functions as a references as a single copy held in memory.

Person.swift
AppDelegate.swift
AppDelegate.swift

=> The original context is changed after it was passed to the function.

Struct is offering less functionality then class. Classes can have inheritance, but structures cannot have inheritance.

The stuct is the best to hold the data, but not the logic.

--

--