Search code examples
iosswiftaccess-modifiersswift-extensionsswiftlint

What is extension_access_modifier swiftlint?


I added Swiftlint to a project and I'm having trouble understanding what the warning is for extension_access_modifier. I see it mainly on a class that is declared as public, but there are extensions littered throughout the codebase that adds functionality.

public class Foo {

}

// In SomeOtherClass.swift
extension Foo { // Extension Access Modifier Violation: Prefer to use extension access modifiers
    public func baz()
}

Whenever there is extension Foo in another class, I get that warning on the extension. Would someone explain what it is?


Solution

  • It's clearer to express that your extension is public, rather than all its members:

    Prefer:

    public extension Foo {
        func bar() { ... }
        func baz() { ... }
        func qux() { ... }
    }
    

    over

    extension Foo {
        public func bar() { ... }
        public func baz() { ... }
        public func qux() { ... }
    }