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 fullName() -> String {
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 fullName()
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 fullName...