Structures, Classes, and Protocols in Swift

Imasha Weerakoon
3 min readMay 26, 2022

If you are an Apple developer you may hear about Swift language. Swift is friendly to new programmers. It’s an industrial-quality programming language that’s as expressive and enjoyable as a scripting language. This article focuses on Structures, Classes, and Protocols in Swift language.

Structures and Classes

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. Swift doesn’t require you to create separate interface and implementation files for custom structures and classes. In Swift, you define a structure or class in a single file, and the external interface to that class or structure is automatically made available for other code to use.

Common things in Structures and Classes both have:

  • Define properties to store values
  • Define methods to provide functionality
  • Define subscripts to provide access to their values using subscript syntax
  • Define initializers to set up their initial state
  • Be extended to expand their functionality beyond a default implementation
  • Conform to protocols to provide standard functionality of a certain kind

Rather than those Classes having more additional capabilities that structures do not have:

  • Inheritance enables one class to inherit the characteristics of another.
  • Typecasting enables you to check and interpret the type of a class instance at runtime.
  • Deinitializers enable an instance of a class to free up any resources it has assigned.
  • Reference counting allows more than one reference to a class instance.

Now, Let’s talk about syntax

Definition Syntax

struct SomeStructure {// structure definition goes here}class SomeClass {// class definition goes here}

Instantiation Syntax

let someStructure = SomeStructure() // struct Instantiationlet someClass = SomeClass() // class Instantiation

Accessing Properties

someStructure.property1 // Accessing properties in structure
someClass.property1 // Accessing properties in class

Choosing between Structures and Classes

Structures and classes are both good options for storing data and modeling behavior in your apps, but their similarity can make choosing one over the other difficult.

The following things are helpful in choosing between Structures and Classes:

  • Use structures by default.
  • Use classes when you need Objective-C interoperability.
  • Use classes when you need to control the identity of the data you’re modeling.
  • Use structures along with protocols to adopt behavior by sharing implementations.

Protocols

A protocol is a set of methods, properties, and other specifications that are tailored to a specific task or piece of functionality. A class, structure, or enumeration can then adopt the protocol to provide an actual implementation of those requirements. Any type that meets the protocol’s requirements is said to conform to that protocol.

You can extend a protocol to implement some of these requirements or to implement additional functionality that conforming types can use, in addition to specifying requirements that conforming types must meet.

Protocol Syntax

Definition Syntax

protocol SomeProtocol {// protocol definition goes here}

Custom types state that they adopt a particular protocol by placing the protocol’s name after the type’s name, separated by a colon, as part of their definition. Multiple protocols can be listed, and are separated by commas:

struct SomeStructure: FirstProtocol, AnotherProtocol {// structure definition goes here}

If a class has a superclass, list the superclass name before any protocols it adopts, followed by a comma:

class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol {// class definition goes here}

This is a very brief introduction to Structures, Classes, and Protocols in Swift. if you want to get more about those go and do some coding in Swift.

“Doing things get more understanding”

Reference:

docs.swift.org

developer.apple.com

--

--