9

I know there are a few questions regarding with this already. And I know swift can only customise property setter and getter for computed properties. But I think this is the worst part of Swift. Because:

  1. All variables are exposed to outside. There is no private or public properties any more.
  2. There is no way to access the "internal" variable of the property like the objective-c, _variable

My code is like this:

var value : Float = 0.0 {

    willSet {
        setValue(newValue, animated: false)
    }
}

func setValue(newValue:Float, animated:Bool) {

    if(newValue != self.value) {
        // TODO: this will cause problem because I there is no alternative way like Objective-c to access _value
        self.value = ....

        // do whatever I want 
    }
}

The problem is the there is no _value like in Objective-c, the self.value will cause the value's willSet be called again.

Any idea? Thanks

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
  • 2
    what is your question? and I consider not able to access underlying variable is a good thing. also related:http://stackoverflow.com/questions/24003918/does-swift-have-access-modifiers – Bryan Chen Jul 08 '14 at 04:39
  • I think Swift should at least support private,protect and public. – Bagusflyer Jul 08 '14 at 05:22
  • that not a question. and Swift will support it at some later stage. – Bryan Chen Jul 08 '14 at 05:25
  • Great to hear that! Then I can use another private variable like what Amadan suggested. – Bagusflyer Jul 08 '14 at 06:36
  • Nice question! Animated properties is a problem in swift. – kelin May 28 '15 at 14:34
  • For the benefit of readers who stumble across this old question, Swift supports `private`, `internal`, and `public` properties. See [Access Control](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID3]. You can even control access for setters and getters separately. – Rob Mar 23 '16 at 19:34

1 Answers1

4

willSet does not define a setter. set does.

var privateValue: Float = 0.0;
var value: Float {
  set(newValue) {
    if newValue != privateValue {
      privateValue = newValue;
      // do whatever I want
    }
  }
  get {
    return privateValue;
  }
}
  1. All variables are exposed to outside. There is no private or public properties any more.
  2. There is no way to access the "internal" variable of the property like the objective-c, _variable

According to my understanding, privateValue will not be accessible anywhere outside the local scope, which would resolve both of your complaints. (EDIT: might be wrong about accessibility; see comments.)

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • local scope? it is class scope and public to outside world (although it can be private when access modifiers are implemented in Swift) – Bryan Chen Jul 08 '14 at 04:42
  • I stand corrected, I thought it was class scope and *not* open to outside world. Still, there are many languages that do not have privacy, like Python; it is easy to get around by just not accessing variables directly, but through a getter/setter. The main point I was trying to make is, `willSet` is not a setter, and the exactly same setter pattern with a backing variable is available in Swift as in other languages. – Amadan Jul 08 '14 at 04:45
  • what do you mean _not_ open to outside world? it _is_ public property. your code is fine for me, but may not be best practice (if such thing for Swift is defined) – Bryan Chen Jul 08 '14 at 04:48
  • @BryanChen: Please read again; that very sentence you quote explains how I admit I might have been wrong. I am not stating that it is not open to outside world, I am stating that it was what I thought, probably under Ruby influence (where all member variables are private, you need an accessor method, or reflection, to access them from outside the class). – Amadan Jul 08 '14 at 04:53
  • sorry I misread your comment. and currently Swift is more like Python (everything is public) rather than Ruby. – Bryan Chen Jul 08 '14 at 05:01
  • @BryanChen: Yep, I read your link. But it feels in so many ways like a child of ObjC and Ruby... – Amadan Jul 08 '14 at 05:08
  • It does like Ruby in some aspect, as well as Haskell, Java, C#, Ruby, Scala... – Bryan Chen Jul 08 '14 at 05:11
  • I know willSet is not setter. But I can't use setter for stored property in Swift. Am I right? You method definitely works but is not as elegant as the other language like ObjC, Java, C# or even C++. But of course that's the limitation of the language itself. – Bagusflyer Jul 08 '14 at 06:35