0

I have a DataManager class and it has some relevant functions and variables. For example,

Class DataManager: NSObject {
   func doSomething()
   func doSomethingAgain()
}

I move few method to an extension of DataManager. I made the extension as fileprivate cause I don't want to expose those function to other classes. For example,

Class DataManager: NSObject {
   func doSomething()
}

fileprivate extension DataManager {
   func doSomethingAgain()
}

Till now everything was fine, now the problem I am facing is when I am moving that extension to a different file that time fileprivate won't work. So what should be the protection level in that case. Hope you understood my intension.

Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
  • 1
    You can't have it in a separate file while still keeping it from being exposed to the rest of the module, there's no access control level for that. See [the docs](https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html) for more details. – John Montgomery Dec 21 '18 at 00:53
  • So is there any workaround to achieve this without creating different module? – Tapas Pal Dec 21 '18 at 10:09

2 Answers2

1

The protection level should be internal (which is the default). It will then be available to everything in the module.

If you want to constrain access to a smaller number of classes, then you need to put those classes in their own module. Those are only access levels that Swift has.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

The tightest access mode you can work with is the internal which is default in Swift.

Taking from https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html :

"Swift provides five different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.

Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.

Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.

File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.

Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration."

Onat Korucu
  • 992
  • 11
  • 13