Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ios development
swift

What are methods in Swift?

Ushna Ijaz

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Methods in Swift are functions that are linked with a particular type. Classes, structures, and enumerations can define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Methods in Swift are similar to functions associated with a particular type.

Syntax

The following is the syntax of a simple method in Swift:

func funcname(Parameters: Datatype) -> returntype {
//Variables,Methods Declaration 
return parameters
}

Types of methods

We have two types of methods in Swift:

  • Instance methods
  • Type methods

Instance methods 

Structures, classes, and enumerations that contain a specific task and functionality with their instances can easily define an instance method. Instance methods are written within the type's body. We write an instance method between the type's opening and closing braces.

An instance method can only be invoked on a specific instance of the type to which it belongs and cannot be invoked in the absence of an existing instance.

Code example

class RectangleArea {
var width = 3
var height = 7
func area() -> Int {
return (width * height)
}
}
var rec = RectangleArea()
var result = rec.area()
print("Area:",result)
Instance method example

Explanation

In the example above, we created a class RectangleArea() with one method, and accessing the class method by creating an instance rec for the class RectangleArea() . This is how we can use instance methods in swift to access class properties or methods.

The self property

Every type of instance has an implicit property called self, which is the same as the instance itself. Within its own instance methods, you use the self property to refer to the current instance. Taking the same example as above, let's see how we can compute using self.

class RectangleArea {
let width: Int
let height: Int
let result: Int
init(num1: Int, num2: Int) {
self.width = num1
self.height = num2
result = num1 * num2
}
}
// calling the function
let rec = RectangleArea(num1: 3, num2: 7)
print("Area:", rec.result)
Using the self property in Swift

Note: There are two types of self used in Swift. Self (when used with a capital S) refers to the type that conform to the protocol, e.g. String or Int. On the other hand, self applies to the current instance, in the body of one of its methods.

Type methods

class TypeMethod {
class func method_name() {
// Body
}
}
TypeMethod.method_name()
Syntax

A type method's implicit self property refers to the type itself rather than an instance of that type. This denotes that you can use self to distinguish between type properties and type method parameters in the same way that you can distinguish between instance properties and instance method parameters.

Code example

class TypeMethod {
static func findMinimum(num1: Int, num2: Int) -> Int {
var min_num: Int = num2
if num1 < num2 {
min_num = num1
}
return min_num
}
}
// calling the function
var min = TypeMethod.findMinimum(num1: 6, num2: 12)
//stored 6 and 12 in num1 and num2 respectively
print(min)
Type method example

Note: To allow subclass overriding of the type method, we can use the class keyword instead of the static keyword.

RELATED TAGS

ios development
swift

CONTRIBUTOR

Ushna Ijaz
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring