[swift-evolution] constant var

Nick Keets nick.keets at gmail.com
Thu Dec 14 03:40:47 CST 2017


What are you going to do about this then?

    func thirdFunc() {

        self.myDict.removeAll()

    }


Is this ok or not? Is it really different from `anotherFunc`?



On Thu, Dec 14, 2017 at 10:52 AM, Inder Kumar Rathore . via swift-evolution
<swift-evolution at swift.org> 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
>
>   }
>
> }
>
> On Tue, Dec 12, 2017 at 10:28 PM, Joe Groff <jgroff at apple.com> wrote:
>
>>
>>
>> > On Dec 11, 2017, at 11:34 PM, Inder Kumar Rathore . via swift-evolution
>> <swift-evolution at 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.
>>
>> -Joe
>>
>>
>
>
> --
> Best regards,
> Inder Kumar Rathore
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171214/b88f6ced/attachment.html>


More information about the swift-evolution mailing list