Difference Between Struct, Protocol, and Class in Swift

Pubudu Wickramathunge
4 min readAug 16, 2022

--

Swift is an open-source, general-purpose programming language developed by Apple Inc. The language is safe, fast, and expressive and It’s intended to be a replacement for C-based languages including Objective-C. Swift is mainly used for native iOS, iPadOS, macOS, tvOS, and watchOS development. In late 2015, the Swift language became open-sourced under the Apache license and Swift.org was created to host the project. Because of that, the Swift source code can be found on GitHub, making it easy for anyone to access the code and contribute to the development. Since Swift language is open-sourced and backed by Apple it is constantly evolving, and the community continues to grow. Swift is a great language to learn If you’re interested in iOS development.

In this article, I'm hoping to give you a clear idea about what is Structure, protocol, and class, and how those differ from each other.

What is a Structure?

Swift provides a flexible building block for making use of constructs as Structures. By making use of these structures one can define the construct's methods and properties. Unlike Objective C Structure need not require implementation files and interface. It allows the developer to create a single file and extend its interface automatically to other blocks. In Structure, the variable values are copied and passed in subsequent codes by returning a copy of the old values so that the values cannot be altered.

Structures use to,

· Encapsulate simple data values.

· Copy the encapsulated data and its associated properties by ‘values’ rather than by ‘references’.

What is a Class?

Classes in Swift are building blocks of flexible constructs. Similar to constants, variables, and functions the user can define class properties and methods. Swift language provides developers the functionality that while declaring classes the users need not create interfaces or implementation files. Once the classes are initialized Swift allows developers to create classes as a single file and the external interfaces will be created by default.

Classes use to,

  • Inheritance acquires the properties of one class to another class
  • Type casting enables the user to check the class type at run time
  • Deinitializers take care of releasing memory resources
  • Reference counting allows the class instance to have more than one reference

What is a Protocol?

Protocols provide a blueprint for Methods, properties, and other requirements functionality. A protocol is just described as a methods or properties skeleton instead of implementation. Methods and properties implementation can further be done by defining classes, functions, and enumerations.

Struct vs Class

Similarities

Classes and Structs in Swift have many similar things such as:

  • Allow defining properties to store values, define methods to provide functionality, and define subscripts to provide access to their values using subscript syntax.
  • Allow defining initializers to set up their initial state.
  • Allow expanding their functionality beyond a default implementation.
  • Allow conforming to protocols to provide standard functionality of a certain kind.

differences

Struct and Classes are pretty similar in Swift. Still, there are quite a lot of important differences to be aware of.

  • One of the most important differences is that a struct is a value type while a class is a reference type.

Struct is the value type object, it is the member-wise initializer. It is called a value type because it passes the value to the function. When you use the struct whole thing is duplicated. It points to the value of the object and then shows the value.

Class is the reference type of object. It passes the reference data to the function. When class is used each object points to the same original object if the developer change one they all will change. It shows the reference to the real object.

  • Structs are allocated in Stack memory.
  • Reference of the class object can be created on the Stack but the properties of the class object are stored in the heap.
  • A class can inherit the property or characteristics of another class. But in struct, it is not possible.
  • Deinitializers are only available on class types. We can de-initialize the class resource at any instant.

Protocol vs Class/ struct

In their basic form, a protocol describes what an unknown type of object can do. It might have two or three properties of various types, and methods. But it never includes anything inside the methods or provides actual storage for the properties.

In comparison, classes are concrete things. While they might adopt protocols. You can create objects from classes, whereas protocols are just type definitions. Think of protocols as being abstract definitions, whereas classes and structs are real things you can create.

Conclusion

Protocols are effectively like interfaces. Classes are classes, like in Java/Android, and pretty much any other language. Structs are like classes, but they are passed by-value. Swift is encouraging users to use struct. So it is better to get used to that concept but in the end, it's all up to you whether to use struct or class.

Hope you find this article helpful. Until we meet with another article, stay safe!

Thank you!

--

--

Pubudu Wickramathunge
Pubudu Wickramathunge

Written by Pubudu Wickramathunge

Software Engineering Undergraduate at University of Kelaniya

No responses yet