Classes & Structure in Swift

Classes and Structures in programming language are two important concepts. Both are useful for building the block of codes and both can have properties and methods. Unlike other programming languages, Swift does not require you to create a separate interface and implementation file. Both interface and implementation can be in a single swift file.

Class and structure have some unique features.

  1. Properties to store values.
  2. Methods to provide functionality
  3. Both can be extended to extend the functionality
  4. Subscripts to access the values
  5. Conform to protocols to provide standard functionality
  6. Initialisers are to set up their initial values

Ok, let’s see what are the significant differences between Structure and class.

  1. Classes have the inheritance properties which make one class can inherit the characteristics from other class. Whereas structure does not have inheritance.
  2. Classes are the reference type, whereas structures are the value type.
  3. Classes have the de-initializer which helps to free up the memory.
  4. Typecasting can be done in classes that will help to identify the class type at runtime.

Syntax:

Both Classes and structures have the same syntax like below.

// Class syntax

class className {

    //properties and methods

}

// Structure  syntax

struct structName {  

  //properties and methods

}

class and struct are the keywords.

Example:

struct Rectangle {  

  var x = 0    

  var y = 0  

  var width = 100  

  var height = 100

}

class Rectangle {  

  var x = 0    

  var y = 0  

  var width = 100  

  var height = 100

}

Class and Structure Instances:

Initialization is the first step to prepare class, structure, and enumeration for use. Initial values can be set during object initialization. Unlike structures and Enumeration classes have de-initializer along with initializer to free up the memory.

let rectangle = Rectangle() // is the initialization for the structure and class

Structures can set the default value while creating an instance for that structure. it can be called as memberwise Initializers

let rectangle = Rectangle(x: 10, y: 10, width: 1000, height: 1000) // it sets the default value to the stored properites such as x, y, width and height.

Accessing Properties:

Properties are in the classes and structures can be accessed using dot(.) syntax based on their access control(private, fileprivate, internal, public).

print("The width of rectangle is \(rectangle.width)")

Complete sample code is here

struct Rectangle {    

var x = 0    

var y = 0    

var width = 100    

var height = 100

}
let rectangle = Rectangle(x: 10, y: 10, width: 1000, height:1000)

print("The width of rectangle is \(rectangle.width))

 

class Rectangle {

    let x = 0

    let y = 0

    let width = 100

    let height = 100

}

let rectangle = Rectangle()

print("The width of rectangle is \(rectangle.width)")

Identity Operators:

We have a dedicated tutorial for swift operators . But explaining about Identity operators here should be the best.  As classes are reference types, properties of different instances of a class may have the same reference in backside. So identity operator helps to check if two or more variables or constants linked to the same reference of a class or not.

=== (three equals) is to check identical and !== is to check not identical

See you on next tutorial, please do sharing and motivate us to write more efficiently.

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *