Struct, Class, and Protocol in Swift

4 min readAug 16, 2022

Apple has been using Objective-C for a long time, however, they released their Swift language in 2014. Swift is a general-purpose, compiled programming language. Software experts who designed Swift borrowed several ideas from Objective-C, Python, Ruby, and C#. Developers can use Swift to code for iOS, macOS, watchOS, and tvOS.It also works seamlessly with the Cocoa and Cocoa Touch frameworks of Apple.com. Swift is the language of choice as far as Apple is concerned, however, Objective-C code can still coexist with Swift code in the same app. In addition to Apple, IBM also backs the Swift programming language.

The first release of Swift was in 2014. But the language rapidly gained popularity because Apple has a huge user base all around the world. Because of that Swift has a bright future ahead of it.

In this article, I’m hoping to give you an idea about classes, structures, and protocols in Swift.

Classes in swift.

A class acts as a blueprint for objects in Swift. Developers can create custom types by implementing classes. A class is used as a factory for creating objects. Unlike other programming languages, we don’t need to create any separate interfaces or implementation files for classes. In swift when we create classes in a single file, automatically that class file will be available for other code blocks to use that functionality in applications.

Structures in Swift,

A structure is a blueprint for creating instances in Swift. You can create custom types with structures. A structure acts as factories for creating instances. In most languages, objects can only be created from classes. But in Swift, you can create objects also from structures and the official documentation advises to prefer structures because they are easier to use and light versions of classes.

A struct can have properties, and methods (functions) and it can define subscripts and initializers. A struct can conform to protocols and it can be extended.

Struct vs Classes

In swift, classes and structures are very similar. They have only a few differences. It is very important to know what the differences are rather than what is similar. Because these are the basic building blocks to making an efficient app.

With that being said let’s begin with the similarity between these two :

  • Properties: This is used for storing data in classes and structures
  • Extensions: We can use extensions to extend our classes or structures.
  • Methods: It provides functionality in our classes or structures
  • Initializer: We use an initializer to create a beginning function once our class or structure has been created. The structure has a free initializer. That means we don’t have to define an initializer function into the structure while classes need it.
  • Subscripts: This provides access to the values using the subscript syntax.

So what about the differences between the two?

  • Type: One of the most distinctions in struct vs class Swift is the type. As mentioned above, struct is a value type, meanwhile, class is a reference type. Reference of class indicates that each instance shares the data when you copy reference type. As a result, The reference itself is copied instead of its data references. In addition, whenever you change one, the other may change. Value of struct, each instance maintains a unique copy of the data. In case you change one instance, the other still keeps.
  • Inheritance: Structure cannot inherit from other types while classes can.
  • Deinitializer: Classes have an initializer to deallocate the occupied space in memory. The structure hasn’t a custom deinitializer.
  • Stored place: While class is stored in the HEAP on the RAM, the structure is stored in STACK on the RAM.
  • Immutability: Structure provides immutability. But class doesn’t provide as well structure.
Main differences between struct and class

Protocols in Swift

Working with protocols is one of Swift’s most fundamental features. With protocols, you define “rules” that an adopting class must conform to. This principle lets you write decoupled, modular, and extensible Swift code.

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. You could say that a protocol defines rules or “requirements”, such as functions and properties. Other classes can then adopt those rules, and provide an actual implementation. Any class that satisfies the rules of a protocol, is said to conform to that protocol.

The power of protocols is that they formalize the connection between different parts of your code, without providing implementations. This allows you to build rigid structures in your code, without tightly coupling your code’s components.

Protocol vs Class and Struct

The biggest difference between classes and protocols is that protocols merely define functions and properties, and don’t implement them.

In a more advanced form, you can write extensions on your protocols that provide default implementations of the methods. You still can’t provide storage for properties, however. In comparison, classes are concrete things. While they might adopt protocols but they aren’t required to do that. You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.

With a protocol, you can define rules that an adopting class needs to conform to. And that has all sorts of benefits, such as letting one class work with another without knowing its exact implementation.

Thank you for reading!

--

--

Umayanga Vidunuwan
Umayanga Vidunuwan

Written by Umayanga Vidunuwan

| Software Engineering Undergraduate| University Of Kelaniya | Sri Lankan|

No responses yet