- Swift - Access Control
- Swift - Generics
- Swift - Protocols
- Swift - Extensions
- Swift - Type Casting
- Swift - Optional Chaining
- Swift - ARC Overview
- Swift - Deinitialization
- Swift - Initialization
- Swift - Inheritance
- Swift - Subscripts
- Swift - Methods
- Swift - Properties
- Swift - Classes
- Swift - Structures
- Swift - Enumerations
- Swift - Closures
- Swift - Functions
- Swift - Dictionaries
- Swift - Sets
- Swift - Arrays
- Swift - Characters
- Swift - Strings
- Swift - Loops
- Swift - Decision Making
- Swift - Operators
- Swift - Literals
- Swift - Constants
- Swift - Tuples
- Swift - Optionals
- Swift - Variables
- Swift - Data Types
- Swift - Basic Syntax
- Swift - Environment
- Swift - Overview
- Swift - Home
Swift Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Swift - Methods
In Swift 4 language Functions associated with particular types are referred to as Methods. In Objective C Classes are used to define methods, whereas Swift 4 language provides the user flexibipty to have methods for Classes, Structures and Enumerations.
Instance Methods
In Swift 4 language, Classes, Structures and Enumeration instances are accessed through the instance methods.
Instance methods provide functionapty
To access and modify instance properties
functionapty related to the instance s need
Instance method can be written inside the {} curly braces. It has imppcit access to methods and properties of the type instance. When a specific instance of the type is called it will get access to that particular instance.
Syntax
func funcname(Parameters) -> returntype { Statement1 Statement2 --- Statement N return parameters }
Example
class calculations { let a: Int let b: Int let res: Int init(a: Int, b: Int) { self.a = a self.b = b res = a + b } func tot(c: Int) -> Int { return res - c } func result() { print("Result is: (tot(c: 20))") print("Result is: (tot(c: 50))") } } let pri = calculations(a: 600, b: 300) pri.result()
When we run the above program using playground, we get the following result −
Result is: 880 Result is: 850
Class Calculations defines two instance methods −
init() is defined to add two numbers a and b and store it in result res
tot() is used to subtract the res from passing c value
Finally, to print the calculations methods with values for a and b is called. Instance methods are accessed with . dot syntax
Local and External Parameter Names
Swift 4 Functions describe both local and global declarations for their variables. Similarly, Swift 4 Methods naming conventions also resembles as that of Objective C. But the characteristics of local and global parameter name declarations are different for functions and methods. The first parameter in Swift 4 are referred by preposition names as with , for and by for easy to access naming conventions.
Swift 4 provides the flexibipty in methods by declaring first parameter name as local parameter names and the remaining parameter names to be of global parameter names. Here no1 is declared by Swift 4 methods as local parameter names. no2 is used for global declarations and accessed through out the program.
class spanision { var count: Int = 0 func incrementBy(no1: Int, no2: Int) { count = no1 / no2 print(count) } } let counter = spanision() counter.incrementBy(no1: 1800, no2: 3) counter.incrementBy(no1: 1600, no2: 5) counter.incrementBy(no1: 11000, no2: 3)
When we run the above program using playground, we get the following result −
600 320 3666
External Parameter Name with # and _ Symbol
Even though Swift 4 methods provide first parameter names for local declarations, the user has the provision to modify the parameter names from local to global declarations. This can be done by prefixing # symbol with the first parameter name. By doing so, the first parameter can be accessed globally throughout the modules.
When the user needs to access the subsequent parameter names with an external name, the methods name is overridden with the help of _ symbol.
class multippcation { var count: Int = 0 func incrementBy(no1: Int, no2: Int) { count = no1 * no2 print(count) } } let counter = multippcation() counter.incrementBy(no1: 800, no2: 3) counter.incrementBy(no1: 100, no2: 5) counter.incrementBy(no1: 15000, no2: 3)
When we run the above program using playground, we get the following result −
2400 500 45000
Self property in Methods
Methods have an imppcit property known as self for all its defined type instances. Self property is used to refer the current instances for its defined methods.
class calculations { let a: Int let b: Int let res: Int init(a: Int, b: Int) { self.a = a self.b = b res = a + b print("Inside Self Block: (res)") } func tot(c: Int) -> Int { return res - c } func result() { print("Result is: (tot(c: 20))") print("Result is: (tot(c: 50))") } } let pri = calculations(a: 600, b: 300) let sum = calculations(a: 1200, b: 300) pri.result() sum.result()
When we run the above program using playground, we get the following result −
Inside Self Block: 900 Inside Self Block: 1500 Result is: 880 Result is: 850 Result is: 1480 Result is: 1450
Modifying Value Types from Instance Methods
In Swift 4 language structures and enumerations belong to value types which cannot be altered by its instance methods. However, Swift 4 language provides flexibipty to modify the value types by mutating behavior. Mutate will make any changes in the instance methods and will return back to the original form after the execution of the method. Also, by the self property new instance is created for its imppcit function and will replace the existing method after its execution
struct area { var length = 1 var breadth = 1 func area() -> Int { return length * breadth } mutating func scaleBy(res: Int) { length *= res breadth *= res print(length) print(breadth) } } var val = area(length: 3, breadth: 5) val.scaleBy(res: 3) val.scaleBy(res: 30) val.scaleBy(res: 300)
When we run the above program using playground, we get the following result −
9 15 270 450 81000 135000
Self Property for Mutating Method
Mutating methods combined with self property assigns a new instance to the defined method.
struct area { var length = 1 var breadth = 1 func area() -> Int { return length * breadth } mutating func scaleBy(res: Int) { self.length *= res self.breadth *= res print(length) print(breadth) } } var val = area(length: 3, breadth: 5) val.scaleBy(res: 13)
When we run the above program using playground, we get the following result. −
39 65
Type Methods
When a particular instance of a method is called, it is called as an Instance method; and when the method calls a particular type of a method, it is called as Type Methods . Type methods for classes are defined by the func keyword and structures and enumerations type methods are defined with the static keyword before the func keyword.
Type methods are called and accessed by . syntax where instead of calpng a particular instance the whole method is invoked.
class Math { class func abs(number: Int) -> Int { if number < 0 { return (-number) } else { return number } } } struct absno { static func abs(number: Int) -> Int { if number < 0 { return (-number) } else { return number } } } let no = Math.abs(number: -35) let num = absno.abs(number: -5) print(no) print(num)
When we run the above program using playground, we get the following result. −
35 5Advertisements