Hamdouchi Interactive
  • Welcome
  • Mobile
  • Web
  • Branding
  • Tech Blog
  • Let's Talk

Structs or Classes: Which Should You Use in Swift?

12/17/2022

1 Comment

 
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
Jonmar
12/17/2022 01:12:32 pm

Great article. Boils things down to the basics in a way that is easy to understand. I hope to see more of these.

Reply

Your comment will be posted after it is approved.


Leave a Reply.

    Picture

    Mohamed Hamdouchi


    Author

    Lead engineer and multimedia innovator interested in just about anything that will keep his gears spinning.


    Archives

    January 2023
    December 2022


    Categories

    All
    Abstract Factory
    Associatetype
    Behavioral
    Bridge
    Builder
    Class
    Creational
    Design Pattern
    Factory
    Factory Method
    Protocol
    Prototype
    Singleton
    Struct
    Structural
    Swift

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