0

In C++ you can specify "public: / private:" sections within your class definitions. Is there a way to do this in Swift 3 or do I have to use the keyword "private" on front of every object I wish to be private?

Makaronodentro
  • 907
  • 1
  • 7
  • 21

1 Answers1

2

If you have

class MyClass {

}

You can declare scoped extensions, e.g.

fileprivate extension MyClass  {
     var someThing: String { // This computed property is fileprivate
         return "ABC"
     }

     func doSomething() {    // This func is fileprivate
     }
}

public extension MyClass  {
     var someOtherThing: String { // This computed property is public
         return "123"
     }

     func doSomethingElse() {     // This func is public
     }
}

However, you can only declare stored properties in your class definition, so this won't work…

private extension MyClass {
    let myName = "Fred"    
}

In this case you need to apply the scope keyword to the property itself…

class MyClass {
    private let myName = "Fred"
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160