iosswiftclass-extensions

iOS - Make swift extension selectively visible


In swift, how do I make an extension visible to only a few classes and not to all?

I have added an extension to UIImage in a file called UIImage+filters.swift. I will be using this extension only in 2 classes in my project PhotoImage and PhotosViewController. I want the extension to be visible only to these 2 classes. How can I achieve this?


Solution

  • That's not possible, access modifiers of classes/extensions behave consistent through all units, separating privileges between outside modules and module where they are. That means that you can declare your class/extension be accesible inside your app/module and not accesible to outsiders or visible to everybody.

    Swift3 comes with a new access modifier, but nothing to restrict access partially inside the module/app.