Swift Structures

In this tutorial you will learn about the Swift Structures and its application with practical example.

Swift Structures

Structure is a user-defined data type, that encapsulate the properties (variables and constant) and methods(functions). It is named type, you can define and five it a name and later you can use it in your. In Swift, Classes and structures are much similar, but one key differences is that classes are reference and structures are value types.

Unlike many other programming languages where structure are mostly used to group related data together, swift structures are fully feature rich and you are allowed to do following things only possible with class –

properties
instance methods
type methods
initializers
subscripts
extensions
protocols

Defining a Structure In Swift

In Swift, structure can be defined using the struct keyword as following –

Syntax:-

Here, structName is replaced with the actual structure name then in between the curly brackets {} we define all the properties and methods associated with the structure.

Example:-

Swift Structure Instances

Once a structure has been defined, we can create instance or objects of that structure which has access to structure properties and methods. In swift, an instance of a structure can be created as following –

Syntax:-

Here, instName and structName is replaced with actual instance name and the structure name respectively.

Example:-

Output:-

swift_structure_instance

Swift Accessing Instance variable and methods

In swift once we have got an instance of a structure created, we can access properties and method of that structure using property/method name separated by a dot (.) operator after the instance name as following.

Syntax for Property:-

Syntax for Method:-

Example:-

Output:-

swift_structure_access_instance_property_method

Swift Structure Initializers

An initializer is a special kind of function in swift, it is used to provide initial values to structure properties while an instance of structure is created.

Example:-

Output:-

swift_structure_initializers

Swift Structures are Value Type

In swift class are of the value types, which means when we assign it to any constants, variables or function an individual copy of structure is created for each of the instance.

Example:-

Here, in the above example we defined a structure employee, we created an instance emp1 with initial value “Alex” for empName property, then we have created another instance emp2 by assigning it previous instance emp1 and updated the value of empName property for emp2 instance, now we printed the value of empName property for both the instances and see that both the instance having individual copy of property.

Output:-

swift_structures_are_value_type

Difference between Classes and Structures

  • Main difference between a class and structure is that classes are reference type and structures are value types.
  • Structures does not provide deinitializers
  • Structures does not provide default mutation
  • Structures does not support type casting

 

 

In this tutorial we have learn about the Swift Structures and its application with practical example. I hope you will like this tutorial.