Learning the Differences Between Structures and Classes in Swift

Structures and Classes in Swift -The Differences Explained

Classes and structures in Swift are multi-faceted and flexible constructs that act as building blocks for your program code. To increase functionality of classes and structures, you can include more properties and techniques using the same syntax as when defining constants, variables, and functions.

In contrast to other programming languages, Swift does not force you to create different files for interfaces and executions of custom classes and structures. In Swift, you build a structure or class in one file, and the external interface is automatically made accessible for use in other code.

An instance of a class is typically called an object. Nevertheless, Swift classes and structures are very similar in functionality than other languages, and much of this chapter defines the functionality that can be applied to instances of both the class and the structure. With than in mind, a more general term is used – an instance.

Comparing Classes and Structures in Swift

As already mentioned, classes and structures in Swift have a lot in common. Using both classes and structures, you can:

  • Include the properties to store values
  • State methods to provide functionality
  • Proclaim indexes to give access to their values using the syntax of the index
  • Raise initializers to set up their initial state
  • They can both be highlighted to extend their functionality beyond the basic execution.
  • They can both follow the same protocols to provide formal functionality of a certain type

Yet classes have additional characteristics that structures don’t have:

  • Inheritance permits one class to inherit the features of another
  • Type casting lets you check and translate the type of an instance of a class at runtime
  • Deinitializers permit an instance of a class to release any resources it has
  • Reference counting allows for more than one reference to an instance of a class.

Additional class support attributes are associated with increased complexity. It is recommended to use structures and enumerations, because they are easier to comprehend. Also, you shouldn’t forget about the classes. Normally, most of the custom data types you operate with are structures and enumerations.

Ad Syntax

Classes and structures in Swift have almost identical declaration syntax. To include classes, use the class keyword, and for structures, use the struct keyword. In both cases, you are expected to put the entire definition completely inside a pair of curly brackets:

class SomeClass {

struct SomeStructure {



Regardless of what you create, a new class or structure, you are actually producing a new type in Swift. Therefore, make sure to assign type names using UpperCamelCase (SomeClass or SomeStructure) to meet the standards for writing type names in Swift (for example, String, Int, and Bool). At the same time, always assign properties and methods names in lowerCamelCase (such as frameRate and incrementCount) to set them apart from type names.

Example of setting structure and class:

struct Resolution {                       
var width = 0                                                                                                                                           
   var height = 0                                                                                                                                 
     class VideoMode {                                                                                                                                     
  var resolution = Resolution()                                                                                                                    
  var interlaced = false                                                                                                                                    
    var frameRate = 0.0                                                                                                                                     
var name: String?                                                                                                                               

The example above calls a new Resolution structure to define the monitor resolution in pixels. This structure has two main characteristics: width, height. Stored characteristics come as either constants or variables that are combined and stored within a class or structure. These characteristics are of the Int type, since we declared them an integer value of 0.

In the same example, we can also see a new VideoMode class used to describe the video mode to show on the video display. The class has four attributes in the form of variables. The first is resolution, specified with an instance of the Resolution structure, which outputs the property type as Resolution. For the other three attributes, the new instance of the class will be established with interlaced = false, frameRate = 0.0, and an optional String value named name. This name property will automatically be assigned the value nil or “no value for name” since it is an optional type.

Defining Instances of the Class and Structure

The proclamation of the Resolution structure and the VideoMode class only determines what Resolution and VideoMode will look like. By themselves, they do not define a particular resolution or video mode. In order to do this, we have to create an instance of a structure or class.

The syntax for building an instance of a class or structure is very similar:

let someResolution = Resolution()     
let someVideoMode = VideoMode()

Both classes and structures add the initializer syntax to build new instances. The standard form of initializer syntax is to use the type name and empty parentheses right after the Resolution () and VideoMode (). This results in a new instance of the class or structure with any initialized properties and their default values.

Accessing Properties

It is best advised to access instance properties using the dot syntax. In the dot syntax, the property name is the one that comes right after the instance name, and a dot (.) is added between them without spaces:

print ("The width of someResolution is \ (someResolution. width)")   
/ / Outputs " The width of some solution is 0"

In the example above, someResolution.width refers to the width property of the someResolution instance, which has an initial value of 0.

You can also go deeper into subproperties, for instance, with the width property or the resolution property that belong to VideoMode class:

print("someVideoMode width is \(someVideoMode.resolution.width)")   
/ / Outputs " someVideoMode width is 0"

As al alternative, you can also use the dot syntax to declare a new value to a property:

someVideoMode.resolution. width = 1280        
print ("someVideoMode width is now equal to \(someVideoMode.resolution. width)")      
/ / Outputs " someVideoMode width is now 1280"

Defining Pointers

In case if you have a previous experience in C, C++ , or Objective-C, then you might know that these languages apply pointers to refer to a memory address. In Swift, constants and variables refer to an instance of some reference type same as C pointers, but they are not direct pointers to a memory address, and they do not request you to add an asterisk(*) to mark that you are making a reference. As a substitute, such references are included as other constants or variables in Swift. The standard library allows you to include pointer and buffer types only if you need to interact directly with pointers.


You may also like...