- A method is a function associated with a type.
- Every method is a function.
Let's consider the following example:
class Foo {
init() {}
func instanceMethod() {
print("An instance method")
}
static func staticMethod() {
print("A type method (static)")
}
class func classMethod() {
print("A type method (class) that can be overridden in a subclass")
}
}
let foo = Foo()
foo.instanceMethod()
Foo.staticMethod()
Foo.classMethod()- An instance method can be called on an instance.
- An instance method cannot be called on a type.
- An type method can be called on a type.
- An type method cannot be called on an instance.
- Type methods can be defined using
staticandclasskeywords:- The
classkeyword is used in classes and actors when you want to override the method in a subclass. - The
statickeyword is used in enums and structs or in classes and actors when you do want to override the method in a subclass.
- The