Читайте также: |
|
- Swift has a great way to monitor changes to the state of our objects by adding something called property observers. These are a very easy way to automatically run some code whenever you change a property value. So taking this simple class definition, I'm going to add observers to this stored property called name. This won't change anything about how we would normally use this property. With name first what I'm going to do is add a code block right at the end just the opening and closing curly brace. Inside that, I can add two more code blocks with the names willSet and after that didSet.
These are our property observers, these are now automatically called for any change to this property. WillSet is called just before it's about to change, and didSet is called just after. You can use one or the other or both or of course, neither. I'm going to add a line to willSet in here, just a printline of about to change name to. What happens is we get passed in an implicit parameter that's called newValue. So willSet knows what it's about to change to.
The flip-side of that is didSet, which gets called afterwards, gets passed in an implicit parameter which is called oldValue. If this property is defined as a string, newValue and oldValue will be a string. If this property is defined as an int, they'll be an integer, and so on. And that's it, that is property observers. Now what I want to see is to see the effect of this we need to make a change to this object. Down here I'm instantiating a new instance of the player class and then what I'll do is I'll go ahead and say newPlayer.name equals Shelly Evans.
We see in the playground just the state of that object but we're not seeing the about to change name to or have changed name from messages. What I need to do is just open up the value history button so I can see the console output. But if I see that, we have the two messages. About to change name to Shelly Evans and have changed name from John Doe. That's new value and the old value that we're seeing. Notice that these are not called for the initialization of the property, they're not called when we first change it to John Doe, the way it was defined in the class itself.
That's because we can always assume the property is given an initial value. Observers take effect after initialization. They are only called when the property is changed. You can, if you want, change the name of the implicit parameter to these blocks if you don't like newValue or oldValue, say if I wanted this to be called new name, all I can do is after the willSet word, I can just put newName in parenthesis that will change the name of the implicit parameter and that's what I'd have to use inside this willSet block.
Having said that, I think newValue and oldValue are a pretty good fit for that information for that data. The only other thing to point out is you cannot add property observers to lazy properties like this one, just two regular stored properties. And that is it for property observers.
Дата добавления: 2015-08-20; просмотров: 115 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Defining lazy properties Определение ленивых свойства | | | Adding property observers Добавление наблюдателей собственности |