From the course: iOS Development Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Lazy variables

Lazy variables

- You might've heard of lazy properties. This week, let's take a look at what they are and how to use them effectively. I have here a simple application in a playground that stores a menu. You can run this. Hmm, I'll just do that like here and click the result button down here and we'll see we get our menu and there's all the pieces of this array. Very simple, okay. Now I'm going to do something that's totally wrong here. I'm going to go up to the top and I'm going to put in var sorted and I'm going to use the sorted function here, the sort names function and I'm going to assign it to a variable like that. And of course we get an error message. And we're going to read the whole error message here. Let's click this and get the whole thing. Cannot use instance members sortNames within property initializer. Property initializers run before self is available. Essentially what we got here is this is still initializing and…

Contents