Читайте также:
|
|
- We have seen that Swift demands that our variables and constants are given meaningful values before they're used. And we've seen a couple of ways to ensure that this happens in our classes. Either with initializers or by providing values in the property declaration like I'm doing here. Sometimes, calculating these initial values as soon as an object is instantiated is not the best use of our applications time. Imagine in our class that we have a property called bonus. And sometimes we'll use this property and sometimes we won't. The problem is here, that the bonus isn't given a literal value it's going to get it's value from another function called getDailyBonus in my example here, that we can assume needs to do some complex processing to figure it out.
Okay, it's really just generating a random number but let's imagine this needs to go out over to the web or get a value from some non-trivial calculation. The thing is, when I instantiate this class what's going to happen is that Swift will go ahead and initialize all these instance level properties and that will cause that function to be called I can even see the results of that in the playground here. I can see that this new object that's been created does have that data inside it. I really don't need to call this function unless I need to access this property.
One way, is I could make it optional using the question mark I could define bonus as an optional int. Then start adding some logic to it so that when I need to access this property will check it for being nil and if it is nil then we'll go fetch that value. But another way, an easier way is that in Swift we can make it a lazy property. With a lazy property we describe the initial value the same way we would normally, but Swift won't bother actually initializing the property until someone attempts to access it.
And it's incredibly easy, all I do is add the word lazy before our var declaration. Now notice the immediate difference without the word lazy. When I'm instantiating the object I can see the results here that this function is being called, it outputs this print line at the top it generates this random number. But if I put that word lazy back in front of it the call to the function disappears it isn't happening right now. The object is describing itself as having a name of John Doe, a score of zero and the bonus is currently nil.
But unlike an optional, I don't have to worry about this bonus being nil, I don't have to check it for being nil, I don't have to unwrap it. Because as soon as I attempt to access a lazy property, I'm just going to do that, as soon as this is instantiated I'm going to go ahead and print out whatever newPlayer.bonus is that is going to cause Swift to immediately initialize it it will call the function, it will get a value and it will return it as if this had a value all along. It will always have a value if I try to use it.
One last thing to point out, lazy properties are always variables. It's always lazy var, it's a compile error if you try and say lazy let. That's because under the hood, the state of this piece of data is actually changing it can begin as nil and then later be given a value. That would not be allowed with a constant which must be given a set value as soon as an object is initialized.
Дата добавления: 2015-08-20; просмотров: 93 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Using type properties and methods Используя свойства и методы типа | | | Defining lazy properties Определение ленивых свойства |