hasan83 hasan83 - 1 year ago 122
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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download