1

Can someone give an example about when it would be recommended to use the modifier public in a class and its properties/methods.

Coming from Java background, it’s a bit confusing because -If I'm not wrong- the equivalent for public modifier in Java would be internal in Swift.

Thanks!

xav
  • 5,452
  • 7
  • 48
  • 57
Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71
  • See here for definitions of `public` and `internal`: https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html – Al Lelopath Aug 25 '14 at 22:02

1 Answers1

2

Swift's access controls are not bound to inheritance, but rather to files and modules. Those modifiers can be applied to types, type members, global variables and constants.

public means it's visible to the outside, similar to Java's public. Those symbols are exported, i.e. the public interface of a Framework.

internal is only accessible to files in the same module. This is similar to package private, but it's not visible to subclasses outside the module. Those are used for things that are not part of the public interface but shared amongst the code inside the Framework.

private symbols are only visible to code in the same file, again independent of inheritance.

jou
  • 5,824
  • 3
  • 22
  • 24
  • By "package private" you mean default visibility? I suppose that in Java's project the fact that the related class are organized in different packages makes it clear the meaning of `public`. – Víctor Albertos Aug 25 '14 at 23:12
  • Yes, I mean default visibility with "package private". A module is usually a library, so it's more like a JAR. You make everything that is accessible to the library's consumer `public`. – jou Aug 26 '14 at 07:26