> On Dec 11, 2017, at 11:34 PM, Inder Kumar Rathore . via swift-evolution 
> <swift-evolution@swift.org> wrote:
> Hi All,
> Today I was writing code and faced a situation where I need to make a 
> instance variable a const i.e. it shouldn't accept new values from anywhere 
> but the problem is that I want it's content to be mutable.
> e.g.
> class MyClass {
>   var myDict = [String : String]()
> }

You can do this by making the setter private:

class MyClass {
  private(set) var myDict = [String: String]()

This will allow declarations inside the MyClass definition to modify myDict, 
but not code outside the class definition.


swift-evolution mailing list

Reply via email to