Swift Methods

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

Swift Methods

In Swift, methods are actually functions associated with classes, structures, and enumerations.Methods describes the behavior for a instance of a classes, structures, or enumerations and mean to provide specific functionality to that type instance and operates on its properties. In swift following two type of methods available –

  1. Instance Methods
  2. Type Methods

Swift Instance Methods

Instance methods are associated with the instances of a specific class, structure, or enumeration. Instance methods are used to perform certain functionality or task for those instances.Instance methods can be invoked using the instance of that type. In swift, instance method can be defined same way as function.

Syntax:-

func :- It is swift a keyword which is used to define a function.
fun_name :- It is replaced with the name of the function .
parameter_list :- It represents the list of the parameters need to be passed when function call made.
return_type :- It represents return type of the function.

Once an instance method is defined it can be invoked using the instance of that class, structure, or enumeration.

Example:-

Output:-

swift_class_access_property_method

Swift Self-Property in Methods

Every instance in swift has an implicit property that refer the current instance within its own instance methods.This implicit property is called as “self” and it is exactly equivalent to the current instance itself.Self property allow us to refer any property or method for the current instance using self keyword followed by dot(.) operator and then property name as following –

Syntax for Property:-

Syntax for Method:-

Example:-

Output:-

swift_access_property_using_self

Swift Modify Value Types from Instance Methods

In swift properties of a structures and enumerations type cannot be modified from within its instance methods because structures and enumerations are value type.

However, if you want to allow structure or enumeration properties to be modified from within its instance method then it can be achieved by opting in for this behavior by placing the mutating keyword before the func keyword for that method.

Example:-

Output:-

swift_modify_value_type

Swift Self Property for Mutating Method

In swift we can assign an entirely new instance to the implicit self property inside a mutating method.

Example:-

Output:-

swift_self_property_mutating_method

Swift Type Methods

Instance methods are called on an instance of a particular type. In swift, we are allowed to define type method that can be called on the type itself.Type methods can be created using static keyword before the a method’s func keyword. Type method can be invoked using dot syntax(.) with the type itself instead if instance.

Example:-

Output:-

swift_type_method

 

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