Class methods access level internal by default

只谈情不闲聊 提交于 2020-01-03 20:03:22

问题


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.


回答1:


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
    } 
}


来源:https://stackoverflow.com/questions/40692367/class-methods-access-level-internal-by-default

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!