Access Control for Swift Extensions Access Control for Swift Extensions swift swift

Access Control for Swift Extensions


Your understanding is almost correct.

A more interesting way to put your scenario 3 would be

extension Test {  // The extension has access control internal, because of what the docs say:  //  // > If you extend a public or internal type, any new type members you add  // > will have a default access level of internal.  //  // Despite the extension being internal, this member is private because it's  // declared explicitly here.  private var prop2: String { return "" }}

as well as

internal extension Test {  // The compiler will give a waning here, why would you define something public  // in an internal extension?  public var prop2: String { return "" }}

Also you might find interesting that if your class, struct or enum is internal, you will not be able to define a public extension. The same goes for a private class, struct or enum, for which you cannot define a public or internal extension.