add a private static method to a class in swift

To add a private static method to a class in Swift, you can use the following syntax:

main.swift
class MyClass {
    // private static method
    private static func myPrivateStaticMethod() {
        // code here
    }
}
124 chars
7 lines

Here, the private keyword ensures that the static method is only accessible within the class scope, and the static keyword specifies that the method belongs to the class and not to an instance of the class.

You can call this private static method within the class using the class name:

main.swift
class MyClass {
    // private static method
    private static func myPrivateStaticMethod() {
        // code here
    }
    
    // another method that calls the private static method
    func myMethod() {
        MyClass.myPrivateStaticMethod()
    }
}
256 chars
12 lines

Note that you cannot access private static methods from outside the class scope, even when using an instance of the class.

gistlibby LogSnag