Static method in Swift

Find this useful? Support us: Star on GitHub 6
Category: Class | Language: Swift

Sure! In Swift, we can define a static method using the static keyword. A static method belongs to the class itself, as opposed to an instance of the class. That means you can call a static method without creating an instance of the class.

Here's an example of how to define a static method in Swift:

class MyClass {
    static func myStaticMethod() {
        print("This is a static method.")
    }
}

In this example, we've defined a static method called myStaticMethod() in a class called MyClass. To call this method, we don't need to create an instance of the class. We can simply call the method directly on the class itself:

MyClass.myStaticMethod() // Output: "This is a static method."

Static methods can also accept parameters, just like instance methods. Here's an example:

class MyClass {
    static func myStaticMethod(name: String) {
        print("Hello, \(name)!")
    }
}

MyClass.myStaticMethod(name: "John") // Output: "Hello, John!"

In this example, we've defined a static method called myStaticMethod() that accepts a name parameter. We can call this method and pass in a value for the name parameter just like we would with any other method. The output will be "Hello, [name]!", where [name] is the value we passed in.