Swift Methods
In Swift, methods are functions that are associated with a particular type, such as a class, structure, or enumeration. They can be used to perform operations on the data of the type or to return a value.
Methods in Swift are defined in the same way as functions, with the func
keyword. They can take parameters and return values, just like functions. The difference is that they are associated with a particular type and can access the properties of that type.
Here is an example of a method:
1class Counter { 2 var count = 0 3 4 func increment() { 5 count += 1 6 } 7}
In this example, the Counter
class has a method called increment()
. This method simply increments the count
property of the Counter
instance.
Swift Static Methods
Static methods in Swift are methods that are associated with a type rather than an instance of the type. They are defined using the static
keyword. Static methods can be useful for performing operations that are not specific to an instance of the type.
Here is an example of a static method:
1struct Math { 2 static func abs(_ number: Int) -> Int { 3 if number < 0 { 4 return -number 5 } else { 6 return number 7 } 8 } 9}
In this example, the Math
structure has a static method called abs()
. This method returns the absolute value of a given number.
Swift Self Property
The self
property in Swift refers to the current instance of a class or structure. It can be used to access the properties and methods of the instance from within a method.
Here is an example of a method that uses the self
property:
1class Person { 2 var name: String 3 4 init(name: String) { 5 self.name = name 6 } 7 8 func sayHello() { 9 print("Hello, my name is \(self.name).") 10 } 11}
In this example, the Person
class has a method called sayHello()
. This method uses the self
property to access the name
property of the current instance.
Swift Mutating Methods
In Swift, structures and enumerations are value types. This means that when you create an instance of a structure or enumeration, a copy of the instance is made. If you want to modify the original instance, you need to use a mutating method.
Mutating methods are defined using the mutating
keyword. They can modify the properties of the instance, but they cannot modify the instance itself.
Here is an example of a mutating method:
1struct Point { 2 var x: Double 3 var y: Double 4 5 mutating func move(x: Double, y: Double) { 6 self.x += x 7 self.y += y 8 } 9}
In this example, the Point
structure has a mutating method called move()
. This method modifies the x
and y
properties of the instance to move the point to a new location.
Feature | Instance Method | Static Method | Self Property | Mutating Method |
---|---|---|---|---|
Defined in | Instance | Type | Instance | Instance |
Use "self" | Yes | No | Yes | Yes |
Modifies struct/class | Yes | No | No | Yes |
Accesses instance vars | Yes | No | Yes | Yes |
Accesses static vars | No | Yes | No | No |
Calls other methods | Yes | Yes | Yes | Yes |
Called by | obj.method() | ClassName.method() | obj.method() | obj.method() |
Methods in Swift are a powerful feature that can be used to perform operations on the data of a type or to return a value. Static methods can be useful for performing operations that are not specific to an instance of the type. The self
property can be used to access the properties and methods of the current instance. Mutating methods are necessary when working with value types, such as structures and enumerations.
Method Type | Syntax | Description | Example |
---|---|---|---|
Instance Method | func methodName(parameters) -> ReturnType { ... } | A method that is called on an instance of a class, structure, or enumeration | func greet(name: String) { print("Hello, \(name)!") } |
Static Method | static func methodName(parameters) -> ReturnType { ... } | A method that is called on the type itself, not on an instance | static func sayHello() { print("Hello, world!") } |
Class Method | class func methodName(parameters) -> ReturnType { ... } | A method that is called on the class, not on an instance | class func max(numbers: [Int]) -> Int { return numbers.max() ?? 0 } |
Self Property | self.propertyName | A property that refers to the current instance of a class, structure, or enumeration | self.name = name |
Mutating Method | mutating func methodName(parameters) { ... } | A method that can modify the instance and its properties | mutating func increment() { self.count += 1 } |
Each row in the table includes the Method Type, Syntax, Description, and an Example of each method.
Instance Methods are called on an instance of a class, structure, or enumeration. They can access and modify the properties of the instance.
Static Methods are called on the type itself, not on an instance. They cannot access or modify the properties of an instance.
Class Methods are called on the class itself, not on an instance. They can access and modify class-level properties.
The Self Property refers to the current instance of a class, structure, or enumeration. It can be used to access and modify instance properties.
Mutating Methods are methods that can modify the instance and its properties. They must be marked with the mutating
keyword.