Skip to main content
updated for Swift 4, thanks @Ahmad F
Source Link
LukeSideWalker
  • 8k
  • 2
  • 43
  • 50

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.

Apple documentation

Swift 3 brought a lot of change including the access levels of instance variables and methods. Swift 3 now has a minimum of 4 access levels:

  • private : entities can only be accessed from within the scope (struct, class, …) where they were defined -> that means also, that from an extension of a class you don't have access to a var of the extended class
  • fileprivate : entities can only be accessed from within the source file where they are defined.
  • internal : entities can be accessed anywhere within the target where they are defined.
  • public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module.

Converting to Swift 3: The default solution changing “private” to “fileprivate” is appropriate in most cases, because the meaning of “private” in swift < 3.0 was like “fileprivate” in Swift >= 3.0

Apple documentation

Swift 3 and 4 brought a lot of change also for the access levels of variables and methods. Swift 3 and 4 now has 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 functions and members can only be accessed from within the scope of the entity itself (struct, class, …) and its extensions (in Swift 3 also the extensions were restricted)
  • fileprivate functions and members can only be accessed from within the source file where they are declared.
  • internal functions and members (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.
  • open or public functions and members can be accessed from anywhere within the target and from any other context that imports the current target’s module.

Interesting:

Instead of marking every single method or member as "private", you can cover some methods (e.g. typically helper functions) in an extension of a class / struct and mark the whole extension as "Private".

class foo { } private extension foo { func somePrivateHelperFunction01() { } func somePrivateHelperFunction02() { } func somePrivateHelperFunction03() { } } 

This can be a good idea, in order to get better maintainable code. And you can easily switch (e.g. for unit testing) to non-private by just changing one word.

Apple documentation

added 2 characters in body; added 8 characters in body
Source Link
LukeSideWalker
  • 8k
  • 2
  • 43
  • 50

Swift 3 brought a lot of change including the access levels of instance variables and methods. Swift 3 now has a minimum of 4 access levels:

private : entities can only be accessed from within the scope (struct, class, …) where they were defined -> that means also, that from an extension of a class you don't have access to a var of the extended class

fileprivate : entities can only be accessed from within the source file where they are defined. internal : entities can be accessed anywhere within the target where they are defined. public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module.

  • private : entities can only be accessed from within the scope (struct, class, …) where they were defined -> that means also, that from an extension of a class you don't have access to a var of the extended class
  • fileprivate : entities can only be accessed from within the source file where they are defined.
  • internal : entities can be accessed anywhere within the target where they are defined.
  • public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module.

Converting to Swift 3: The default solution changing “private” to “fileprivate” is appropriate in most cases, because the meaning of “private” in swift < 3.0 was like “fileprivate” in Swift >= 3.0

Apple documentation

Swift 3 brought a lot of change including the access levels of instance variables and methods. Swift 3 now has a minimum of 4 access levels:

private : entities can only be accessed from within the scope (struct, class, …) where they were defined -> that means also, that from an extension of a class you don't have access to a var of the extended class

fileprivate : entities can only be accessed from within the source file where they are defined. internal : entities can be accessed anywhere within the target where they are defined. public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module.

Converting to Swift 3: The default solution changing “private” to “fileprivate” is appropriate in most cases, because the meaning of “private” in swift < 3.0 was like “fileprivate” in Swift >= 3.0

Apple documentation

Swift 3 brought a lot of change including the access levels of instance variables and methods. Swift 3 now has a minimum of 4 access levels:

  • private : entities can only be accessed from within the scope (struct, class, …) where they were defined -> that means also, that from an extension of a class you don't have access to a var of the extended class
  • fileprivate : entities can only be accessed from within the source file where they are defined.
  • internal : entities can be accessed anywhere within the target where they are defined.
  • public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module.

Converting to Swift 3: The default solution changing “private” to “fileprivate” is appropriate in most cases, because the meaning of “private” in swift < 3.0 was like “fileprivate” in Swift >= 3.0

Apple documentation

Source Link
LukeSideWalker
  • 8k
  • 2
  • 43
  • 50

Swift 3 brought a lot of change including the access levels of instance variables and methods. Swift 3 now has a minimum of 4 access levels:

private : entities can only be accessed from within the scope (struct, class, …) where they were defined -> that means also, that from an extension of a class you don't have access to a var of the extended class

fileprivate : entities can only be accessed from within the source file where they are defined. internal : entities can be accessed anywhere within the target where they are defined. public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module.

Converting to Swift 3: The default solution changing “private” to “fileprivate” is appropriate in most cases, because the meaning of “private” in swift < 3.0 was like “fileprivate” in Swift >= 3.0

Apple documentation