2 Replies
      Latest reply: Oct 28, 2016 4:46 AM by LordOfAwe RSS
      LordOfAwe Level 1 Level 1 (0 points)

        Isn't there a way to make every member of a class be private or fileprivate by default?

        I would rather have everything private by default, and just mark the funcs and vars that I want other parts of my program to see.

         

        Much the way objectiveC did actually. Where I would only put the public stuff on the header file.

         

        I realize not everybody would like this, so I suppose swift lets us choose our way.

         

        So is there a way to let the programmer specify the rule that he prefers at the beggining of every file like:

        @default_to_private or @private_members or @private_by_default or something?

         

        Or maybe there is a build setting for that, that changes the behaviour on all files of the project.

        Please let me know.

        • Re: class members are private by default
          eskimo Apple Staff Apple Staff (7,530 points)

          What I usually do is put my public API in the class and my private API in extension:

          public class Blob {
              public init(sqishiness: Int) {
                  self.sqishiness = sqishiness
              }
              public let sqishiness: Int
          }
          
          private extension Blob {
              func simulatorFluidDynamics() {
              }
          }
          

          Note I have to explicitly flag init(sqishiness:) and sqishiness as public because they default to internal.

          The only gotcha with this appropriate is that store properties have to be declared within the class, so you have to explicitly flag those as private.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"