<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">If I understand correctly, you want to be able to modify <font face="Menlo" class="">myDict</font> from within <font face="Menlo" class="">MyClass</font>, but not from outside it. In that case, you’re looking for <font face="Menlo" class="">private(set)</font>.<div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">class MyClass {</font></div><div class=""><font face="Menlo" class=""> private(set) var myDict = [String : String]()</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class="">-BJ</div><div><br class=""><blockquote type="cite" class=""><div class="">On Dec 12, 2017, at 12:34 AM, Inder Kumar Rathore . via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi All,<div class="">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.</div><div class=""><br class=""></div><div class="">e.g.</div><div class=""><br class=""></div><div class="">class MyClass {</div><div class=""> var myDict = [String : String]()</div><div class="">}</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">I want above variable to be constant and if I make it like below</div><div class=""><br class=""></div><div class=""><div class="">class MyClass {</div><div class=""> let myDict = [String : String]()</div><div class="">}</div><div class=""><br class=""></div><div class="">Then I cann't add key/value in the myDict like</div><div class=""><br class=""></div><div class=""> self.myDict["name"] = "Rathore"</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">I know swift and couldn't find anything related to this.</div><div class=""><br class=""></div><div class="">Can anybody help me?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">If there is no such method of doing it then I would suggest to either use a syntax like</div><div class=""><br class=""></div><div class=""><div class="">class MyClass {</div><div class=""> const var myDict = [String : String]()</div><div class="">}</div></div><div class=""><br class=""></div><div class="">I'm not using <i class="">final </i>here since that make a var not overridable.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature"><div dir="ltr" class=""><font color="#666666" class="">Best regards,</font><div class=""><font color="#666666" class="">Inder Kumar Rathore</font></div></div></div>
</div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>