9

I know how to set 'setter' of an stored property to private (e.g. public private(set) var name: String = "John") but how do we set 'setter' of an computed property to private? In this case the 'setter' for the variable 'age'. When I tried to put an keyword private in front of set(newAge){}, XCode display an error. So is it possible to set 'setter' of an computed property to private?

public class Person {

    public private(set) var name: String = "John"

    var age: Int{
        get {
            return 10
        }
        set(newAge){ // how to set this setter to private so to restrict modification

        }
    }
}
Thor
  • 9,638
  • 15
  • 62
  • 137

1 Answers1

23

You do it the same way as for a stored property:

    private(set) var age: Int{
        get {
            return 10
        }
        set(newAge) {
            // setter code here
        }
    }
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • awesome!! thank you so so much! This has been puzzling me for ages! now thanks to you, I finally got it! – Thor Mar 14 '16 at 22:46
  • Note that you can just write `set {` without naming the new value, and the new value will be accessible inside the curly brackets as `newValue`. – BallpointBen Jul 07 '17 at 14:20