Swift Classes

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

Swift Classes

Class can be defined as a blueprint or prototype of associated objects. Class wrapper that binds the data and methods which can be later accessed by objects of that class. We can think of a class as a user-defined data type, that describes the behavior and characteristics shared by all of its instances.

Defining a Class In Swift

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

Syntax:-

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

Example:-

Swift Class Instances

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

Syntax:-

Here, objectName and className is replaced with actual object name and the class name respectively.

Example:-

Output:-

swift_class_objects

Swift Accessing Instance variable and methods

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

Recommended:-  Swift Fallthrough Statement

Syntax for Property:-

Syntax for Method:-

Example:-

Output:-

swift_class_access_property_method

 

Swift Class Identity Operators

In swift class instances are of the reference types, which means all of the class objects shares memory reference to the class store instead of having an copy of class. There may be possibilities that multiple constants and variables will refer the same class instance.

In Swift, we have a set of identity operators allow us to check two constants and variables referring to same instance of a class or not.

Operator Description
Identical Operator(===) It returns true when two constants or variables pointing to a same instance
Not Identical Operator used is (!==) It returns true when two constants or variables pointing to a different instance

Example:-

Output:-

swift_class_identical_operators

Swift Classes are Reference Type

In swift class are of the reference types, which means when we assign it to any constants, variables or function reference to same existing instance will be used instead of an individual value copy of class.

Recommended:-  Swift Advanced Operators

Example:-

Here, in the above example we defined a class 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 the value is updated for both the instance because class are reference type and both instances having reference to the same class instance.

Output:-

swift_class_are_reference_type

 

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