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 –
- Instance Methods
- 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:-
1 2 3 4 |
func fun_name(parameter_list) -> return_type { statements return statement } |
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class employee { var empName: String = "John" var empAge: Int = 35 var empSalary: Int = 5000 func showEmpName(){ print("Employee name is : \(emp.empName)") } } let emp = employee() var empSalary:Int = emp.empSalary print("W3Adda - Swift Access Class Property and Method") emp.showEmpName() print("Salary of \(emp.empName) is \(empSalary)") |
Output:-
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:-
1 |
self.propName |
Syntax for Method:-
1 |
self.methodName() |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class employee { var empName: String = "John" var empAge: Int = 35 var empSalary: Int = 5000 func showEmpName(){ print("Employee name is : \(self.empName)") print("Employee age is : \(self.empAge)") print("Employee salary is : \(self.empSalary)") } } let emp = employee() var empSalary:Int = emp.empSalary print("W3Adda - Swift Access Property and Method using Self") emp.showEmpName() |
Output:-
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
struct Counter { var ctr = 10 func getCtrNormal() -> Int { return self.ctr } mutating func getCtrMut(c:Int)->Int{ ctr=ctr+c return ctr } } print("W3Adda - Swift Modify Value Types from Instance Methods") var ctr = Counter() print(ctr.getCtrNormal() ) print(ctr.getCtrMut(c:10) ) print(ctr.getCtrNormal()) var ctr1 = Counter() print(ctr1.ctr) |
Output:-
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
enum TrafficLight { case red, yellow, green mutating func next() { switch self { case .red: self = .yellow case .yellow: self = .green case .green: self = .red } } } print("W3Adda - Swift Self Property for Mutating Method") var signal = TrafficLight.yellow signal.next() print(signal) // signal is now equal to .green signal.next() print(signal) // ovenLight is now equal to .red |
Output:-
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class MathOps { class func sum(num:Int)->Int { return (10 + num) } } struct MathOpsStruct{ static func multiply(num:Int)->Int { return (10 * num) } } let res_sum = MathOps.sum(num:10) let res_mul = MathOpsStruct.multiply(num:5) print("W3Adda - Swift Type Method") print(res_sum) print(res_mul) |
Output:-