> On Dec 14, 2017, at 12:51 AM, Inder Kumar Rathore . <rathore...@gmail.com> > wrote: > > class MyClass { > private var myDict = [String : String]() > > func addMemebr() { > self.myDict["key"] = "value" // Ok for me > } > > func anotherFunc() { > self.myDict = [String : String]() // Not okay for me, I don't want any > code to do this within the class > } > }
If you want code to be able to insert, but not delete from or otherwise freely change, the dictionary value, you could provide a wrapper type for myDict that provides a restricted mutation API: struct InsertOnlyDictionary { private(set) var value = [String: String]() mutating func insert(_ k: String, _ v: String) { value[k] = v } } // Code outside of InsertOnlyDictionary can read its `value`, but can only modify it by its `insert` API class MyClass { private var myDict = InsertOnlyDictionary() func addMember() { self.myDict.insert("key", "value") } } -Joe
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution