Methods

Methods are functions that are associated with an instance of a class or structure. A method, like a function, will encapsulate the code for a specific task or functionality that is associated with the class or structure. Let's look at how we can define methods for classes and structures. The following code will return the full name of the employee by using the firstName and lastName properties:

func getFullName() -> String { 
  return firstName + " " + lastName 
} 

We define this method exactly as we would define any function. A method is simply a function that is associated with a specific class or structure, and everything that we learned about functions in the previous chapters applies to methods. The getFullName() function can be added directly to the EmployeeClass class or EmployeeStruct structure without any modification.

To access a method, we use the same dot syntax we used to access properties. The following code shows how we access the getFullName() method of a class and a structure:

var e = EmployeeClass() 
var f = EmployeeStruct(firstName: "Jon", lastName: "Hoffman", salaryYear: 50000) 
 
e.firstName = "Jon"  
e.lastName = "Hoffman"  
e.salaryYear = 50000.00 
 
print(e.getFullName()) //Jon Hoffman is printed to the console  
print(f.getFullName()) //Jon Hoffman is printed to the console

In the preceding example, we initialize an instance of both the EmployeeClass class and EmployeeStruct structure. We populate the structure and class with the same information and then use the getFullName() method to print the full name of the employee to the console. In both cases, Jon Hoffman is printed to the console.

There is a difference in how we define methods for classes and structures that need to update property values. Let's look at how we define a method that gives an employee a raise within the EmployeeClass class:

func giveRaise(amount: Double) {  
  self.salaryYear += amount 
} 

If we add the preceding code to our EmployeeClass, it works as expected and when we call the method with an amount, the employee gets a raise. However, if we try to add this method as it is written to the EmployeeStruct structure, we receive a mark method mutating to make self mutable error. By default, we are not allowed to update property values within a method of a structure. If we want to modify a property, we can opt into mutating behavior for that method by adding the mutating keyword before the func keyword of the method declaration. Therefore, the following code would be the correct way to define the giveRaise(amount:) method for the EmployeeStruct structure:

mutating func giveRase(amount: Double) {  
  self.salaryYear += amount 
} 

In the preceding examples, we use the self property to refer to the current instance of the type within the instance itself, so when we write self.salaryYear, we ask for the value of the salaryYear property for the current instance of the type.

The self property should only be used when necessary. We are using it in these past few examples to illustrate what it is and how to use it.

The self property is mainly used to distinguish between local and instance variables that have the same name. Let's look at an example that illustrates this. We can add this function to either the EmployeeClass or EmployeeStruct type:

func isEqualFirstName(firstName: String) -> Bool {  
  return self.firstName == firstName 
} 

In the preceding example, the method accepts an argument named firstName. There is also a property within the type that has the same name. We use the self property to specify that we want the instance property with the name firstName, and not the local variable with this name.

Other than the mutating keyword being required for methods that change the value of the structure's properties, methods can be defined and used exactly as functions are defined and used. Therefore, everything we learned about functions in the previous chapter can be applied to methods.

There are times when we want to initialize properties or perform some business logic when a class or structure is first initialized. For this, we will use an initializer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset