Hamdouchi Interactive
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk

Unlocking the Power of Swift:
A Tech Blog Series

Welcome to our tech blog series dedicated to exploring the world of Swift and its related topics. Our goal is to provide valuable insights and in-depth analysis on the latest advancements in the Swift programming language, including design patterns and data structures. Stay tuned for our upcoming articles and join the discussion on the exciting world of Swift programming!

Explore all Swift Topics

Structs or Classes: Which Should You Use in Swift?

12/17/2022

1 Comment

 
Picture
When it comes to designing classes and structures in Swift, there are a few key differences to consider. In general, a class is a reference type, meaning it is stored and passed by reference, whereas a structure is a value type, meaning it is copied and passed by value.

One key difference between a class and a structure is the ability to inherit from a parent class. Classes can inherit from other classes, allowing for hierarchical relationships and reuse of code. Structures, on the other hand, cannot inherit from other structures or classes.

Another difference is the ability to use deinitializers. Classes can have deinitializers, which are called when an instance of the class is no longer needed and can be used to free up resources. Structures do not have this ability.
​
When deciding whether to use a class or a structure in your Swift code, consider the following:
  • If you need to inherit from a parent class, use a class.
  • If you need to use a deinitializer, use a class.
  • If you need value semantics, meaning the data should be copied and passed by value, use a structure.
  • If your data is small and will not be passed around extensively, use a structure.

​Overall, it is important to carefully consider the needs of your project when deciding whether to use a class or a structure in your Swift code. Both have their advantages and disadvantages, and the choice ultimately depends on the specific requirements of your project.
In addition to the points mentioned above, there are a few other key considerations when deciding between using a class or a structure in Swift.

One important factor to consider is mutability. Structures are always copied when they are passed around, which means that any changes made to the structure will not affect the original instance. Classes, on the other hand, are reference types, so any changes made to an instance will be reflected in all references to that instance.

Another factor to consider is type casting. Classes can be type cast to other classes in the same hierarchy, whereas structures cannot be type cast.

Finally, consider the impact on performance. Because structures are value types and are always copied, they can be more efficient in some cases, especially when working with small amounts of data. Classes, on the other hand, may be more efficient when working with large amounts of data or when multiple references to the same instance are needed.

In general, structures are a good choice for small, lightweight data types, while classes are better suited for complex data types that may need to be inherited from or type cast to other classes. It is important to carefully evaluate the needs of your project and choose the appropriate type based on those needs.
​Here are some code examples to illustrate the differences between classes and structures in Swift:

Example of a class:

    
​In this example, Person is a class with two properties, name and age, and an initializer to set those values. It also has a method, introduce(), which prints a message with the person's name and age.

When we create an instance of Person, person1, and assign it to person2, any changes made to person1 will be reflected in person2 because classes are reference types.

Example of a structure:

    
​In the first example, the Person class is defined with a name and age property, as well as an initializer and a introduce() method. When the person1 and person2 instances are created, person2 is a reference to person1, so when the age property is changed on person1, it is also changed on person2.

In the second example, the Point structure is defined with x and y properties, as well as a moveBy() method that changes the values of the properties. When the point1 and point2 instances are created, point2 is a copy of point1, so when the moveBy() method is called on point1, it does not affect point2.
1 Comment
<<Previous
    Picture

    Mohamed Hamdouchi


    Author

    Lead iOS Engineer.
    I help develop Design System Libraries.
    Creative Thinker.
    Featured on the App Store with 4M+ downloads.


    Archives

    November 2023
    October 2023
    August 2023
    July 2023
    June 2023
    May 2023
    April 2023
    March 2023
    February 2023
    January 2023
    December 2022


    Categories

    All
    Abstract Factory
    Animation
    Array
    Associatetype
    Avl
    Behavioral
    Bridge
    Builder
    Case
    Chaining
    Chain Of Responsibility
    Class
    Closure
    Coalescing
    Coercion
    Command
    Composite
    Computed Property
    Conditional Conformance
    Crash
    Creational
    Data Structure
    Decorator
    Default Case
    Design Pattern
    Dictionary
    Enum
    Extension
    Facade
    Factory
    Factory Method
    Flyweight
    Function
    Generics
    Graphs
    Guard
    Hash Table
    Heap
    Initialization
    Interpreter
    Ios
    Iterator
    Linked List
    Lowercase
    Mapping
    Mediator
    Memento
    Observer
    Optional
    Pattern Matching
    Protocol
    Prototype
    Proxy
    Quadratic Probing
    Queue
    Search
    Self
    Sequence
    Singleton
    Stack
    State
    Strategy
    Strikethrough
    Struct
    Structural
    Subscript
    Swift
    Template Method
    Text
    Tree
    Tries
    Try
    UIKit
    Uilabel
    UITableView
    Uppercase
    Visitior

​​COPYRIGHT © 2009 HAMDOUCHI INTERACTIVE, LLC. ​ALL RIGHTS RESERVED
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk