Search code examples
swiftaccess-controlclass-diagram

Swift Access Control When to Use Which Preceding Symbols For a Class Diagram


I have learned about class diagrams in Java before; However, I would like to use them properly at work with Swift too. I am not looking for anything autogenerated because I want to get good at drawing them and reading them quickly.

Anyways I am pretty confident for Java we use the preceding symbols to represent variables visibility. (Picture example)

+   Public
-   Private
#   Protected
~    Package
*   Random    

But when it comes to Swift I am a little confused where to find a proper consensus of which symbol to use when I have an implicit internal variable inside of a public class like so.

public class MapTracker {
    var locationManager = CLLocationManager()
}

The Swift 4.2 Documentation for Access Control has me thinking that this is closely related to either Package or Protected.

Any ideas?


Solution

  • As you have noticed, Java is really different from Swift. :)

    Swift has the following access modifiers, from the most accessible to the least:

    • open
    • public
    • internal
    • fileprivate
    • private

    They all behave quite differently from their Java counterparts (if any). So my suggestion is to not try to find equivalents of them in Java. Forget everything you know about Java and try to come up with symbols that are specific to Swift. For example, I came up with:

    ++    open
    +     public
    ~     internal
    -~    fileprivate
    -     private
    

    You can also argue that it is not quite useful to differentiate fileprivate from private because in a class diagram, you can't see which classes belong to which files. You can use - for both of them.


    I found this project that generates a class diagram of your swift code. From the looks of it, it doesn't seem to generate anything that indicates the access modifier.