Swift Extensions and Information Hiding
Andrew Bancroft tested and analyzed the behavior of extensions in Swift. While his findings aren’t utterly surprising, having them summed up in this nicely done article certainly helps.
In a nutshell:
- extensions in the same file as their source can access even
privatmembers (attributes and methods, that is) - extensions in different files can access
internalandpublicmembers – but only if they are in the same module - if extensions are in a different module, they merely have access to
publicmembers - classes with
publicorinternalvisibility on themselves expose their members asinternalby default - classes with
privatevisibility on themselves expose their members as private - extensions themselves don’t have to be
publicforpublicmembers to work (unlike classes)
This has consequences for your tests: they reside in a separate module, so they can only access public members of your classes.
If you extend classes from static libraries, the same holds true. Different module, only public accessors.