Swift 3 and 4 brought a lot of change includingalso for the access levels of instance variables and methods. Swift 3Swift 3 and 4 now has a minimum of 4 different access levels, where open/public access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level:
- private : entitiesfunctions and members can only be accessed from within the scope of the entity itself (struct, class, …) where theyand its extensions (in Swift 3 also the extensions were defined -> that means also, that from an extension of a class you don't have access to a var of the extended classrestricted)
- fileprivate : entitiesfunctions and members can only be accessed from within the source file where they are defineddeclared.
- internal functions and members : entities(which is the default, if you do not explicitly add an access level key word) can be accessed anywhere within the target where they are defined. Thats why the TestTarget doesn't have automatically access to all sources, they have to be marked as accessible in xCode's file inspector.
- publicopen or public entitiesfunctions and members can be accessed from anywhere within the target and from any other context that imports the current target’s module.
Converting to Swift 3Interesting:: The default solution changing “private” to “fileprivate” is appropriate in most cases, because the meaning
Instead of “private”marking every single method or member as "private", you can cover some methods (e.g. typically helper functions) in swift < 3an extension of a class / struct and mark the whole extension as "Private".0 was like “fileprivate”
class foo { } private extension foo { func somePrivateHelperFunction01() { } func somePrivateHelperFunction02() { } func somePrivateHelperFunction03() { } } This can be a good idea, in Swift >= 3order to get better maintainable code.0 And you can easily switch (e.g. for unit testing) to non-private by just changing one word.