ProgrammingiOS Developer

How do structures (struct) work in Swift, what is the difference from class objects (class) in terms of storage and behavior, and why are structures more commonly used for data modeling?

Pass interviews with Hintsage AI assistant

Answer.

Context

In Swift, value types — structures (struct) are emphasized as the primary tool for data modeling. Unlike Objective-C, where everything was objects (classes), Swift encourages the use of structures for simple models, data, and small business objects.

Problem

Many developers, especially those experienced in other object-oriented languages, mistakenly use classes for everything. This leads to memory issues (reference cycles), unexpected changes when passing objects, and complexities with thread safety, as classes are reference types.

Solution

Structures in Swift are value types; they are copied upon assignment and when passed to functions, unlike classes (reference types), which pass a reference. This makes structures preferable for models without complex lifecycle logic and inheritance.

Code example:

struct Point { var x: Int var y: Int } var p1 = Point(x: 10, y: 20) var p2 = p1 p2.x = 30 // p1.x remains 10

Key features:

  • Copying a structure always invokes a copy of values (value semantics)
  • Fewer memory leak possibilities
  • Do not support inheritance, only protocols

Trick questions.

Can a structure have a subclass?

No, structures in Swift do not support inheritance. Any extension of behavior is only possible through protocols and extensions.

Does this mean that structures are always copied when passed to a function?

In practice, Swift uses Copy-On-Write optimizations. If we do not modify the structure, it is not copied, and a copy is created only upon change. This applies to standard collections and complex structures.

var arr1 = [1, 2, 3] var arr2 = arr1 arr2.append(4) // Only here does the copying happen

In what cases should structures not be used?

  • If identity (objectness, reference comparison) is required
  • If inheritance is needed
  • If there should be a single instance (singleton)

Common mistakes and anti-patterns

  • Using classes for simple models and data structures
  • Storing reference types inside structures and trying to copy them
  • Expecting to pass the structure "by reference" and trying to modify the external object through the structure

Real-life example

Negative case

Classes were used to store uniform data (for example, for points on a map), resulting in performance losses due to frequent memory access, complicated memory management, and bugs with mixed references.

Pros:

  • Ability to store in an array as AnyObject

Cons:

  • Lower performance
  • Difficulties with thread safety
  • Memory management issues

Positive case

Using structures for data models that copy safely, do not lead to leaks, and have no unnecessary complexity.

Pros:

  • Simplicity, safety in copying
  • Absence of reference cycles
  • Easy to test

Cons:

  • Cannot implement patterns requiring inheritance
  • If there is a reference type inside the structure — surprises may occur during copying and changes