hasan83 hasan83 - 15 days ago 5
iOS Question

Class methods private by default in Swift

Usually your class have many methods. Its annoying to set the access modifier for all of them as private and keep one or two with no access modifier.

Is there a way to let all methods private by default and give the access modifier to the public ones? maybe by assigning an access modifier for the class.

Answer

You move all your private methods to an extension in the same file and mark it as fileprivate.

class Foo {
    // public stuff, stored properties etc.
}

fileprivate extension Foo {
    // private methods, computed properties etc.
    func bar() {
        // this method is fileprivate
    } 
}
Comments