Structure, Protocol, and Class in Swift: The differences explained.

Sureshkumar kajanthan
4 min readMay 30, 2022

First of all, we know Swift……..

Swift was introduced in 2014 at Apples's Worldwide Developers Confrence.

The development of Swift was started in 2010 by Chris Lattner with other programmers i.e. Doug Gregor, John McCall, Ted Kremenek, and Joe Groff at Apple. The basic idea of Swift was taken from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and many other programming languages.

Swift first version was 1.0 which undergone a major upgrade to Swift 2 at WWDC 2015. Its upgraded version 2.2 was made open-source software under the Apache License 2.0 on December 3, 2015, for Apple's platforms and Linux.

Its third version, Swift 3.0 went through a significant evolution where its syntax was changed. Swift overcame the popularity of Objective C in the first quarter of 2018.

what is SWIFT?

Swift is a general purpose, multi-paradigm, compiled programming language for developing iOS and OS X tvOS, watchOS applications. It is developed by Apple Inc. It is powerful and intutive language which is easy to learn. Swift code is safe, precise and runs very fast.

Swift follows Objective-C runtime library which allows C, Objective-C, C++ and Swift code to run within one program. Swift is built with open source LLVM compiler and included in Xcode since version 6.

I am going to explain differences between Structure, Protocol, and Class in Swift.

what is the class in Swift?

Classes in Swift are similar to Structures in Swift. These are building blocks of flexible constructs. You can define class properties and methods like constants, variables and functions are defined. In Swift 4, you don’t need to create interfaces or implementation files while declaring classes. Swift 4 facilitates you to create classes as a single file and the external interfaces will be created by default when the class is initialized.

  • properties
  • methods
  • subscripts
  • initializers
  • protocol conformances
  • extensions

what is structure in Swift?

Swift structures are the flexible basic building blocks of the programs. The “struct” keyword is used to define structures. By using structures, you can define constructs methods and properties.

  • properties
  • methods
  • subscripts
  • initializers
  • protocol conformances
  • extensions

what is protocol in Swift?

In Swift, a protocol defines a blueprint of methods or properties that can then be adopted by classes (or any other types).

  • The protocol just holds the method or properties definition, not their actual body.
  • The protocol must specify whether the property will be gettable or gettable and settable.

Class vs Structure

Structures and classes are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your structures and classes using the same syntax you use to define constants, variables, and functions.

  • Classes can inherit from another class, as you inherit from UIViewController to create your own view controller subclass, but struct can’t.
  • Classes are reference types and structs are value types.
  • Typecasting enables you to check and interpret the type of a class instance at runtime.
  • In class, a Shared mutable state is required, and in struct, unique copies with an independent state are required.
  • In class, Objective-C interoperability is required and in a struct, the data is used in multiple threads.

Class vs Protocol

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

  • Classes are concrete things. While they might adopt protocols — i.e., say they implement the required properties and methods — they aren’t required to do that.
  • You can create objects from classes, whereas protocols are just typed definitions.
  • Protocols are like abstract definitions, whereas classes and structs are real things you can create.

Structure vs Protocol

  • In Swift, protocols provide communication across unrelated objects by defining methods and variables similar to those found in classes, enums, and structs.
  • Protocols are similar to interfaces, and Structs are similar to classes, except they are passed by value from one variable/function to the next.

--

--