LoginSignup
2
1

More than 5 years have passed since last update.

Swift - Lazy Property (lazy var)

Posted at

I. INTRODUCTION

Hey , Today our topic will be lazy property. Some people they known it as lazy var. So if we talk about the lazy property, we will think about the memory used by application .What do I mean by "Memory Used by application" ? Well , When you develop an application of course we have to create object , variable , etc... to store the user data or input. So if you are developing a small or medium application I think you are going to be ok but if you develop a large or complex application I am sure you will face the memory issue problem if you are not consider about the memory allocation.

II. " Lazy Property? What is that ? "

Apple official document say: "A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration. "

to make above statement more clear please see the example code below.

struct InterviewCandidate {
    var isiOS:Bool?

    lazy var iOSResumeDescription: String = {
        return "I am an iOS developer"
    }()
    lazy var androidResumeDescription: String = {
        return "I am an android developer"
    }()
}

var person1 = InterviewCandidate()
person1.isiOS = true

if person1.isiOS! {
    print(person1.iOSResumeDescription)
} else {
    print(person1.androidResumeDescription)

}

So in above code , as you can see.
1. We have 1 struct name InterviewCandidate which has 1 Bool variable and 2 lazy var as it property.
2. We have the struct property object name person1 .
3. During if else statement . it is going to invoke the lazy var name IOSResumeDescription and it will return "I am an IOS developer" as its value. On the other hand the value of another lazy var which name androidResumeDescription will be nil .

why? why nil?

because as you can see the above code , the if statement is true so the lazy var iOSResumeDescription going to initialize when the program execute print statement and the program is not going to execute else statement so it is not going to invoke lazy var androidResumeDescription. By doing that you could save the memory allocate of lazy var androidResumeDescription.

III. Conclusion

So by using lazy property you could save memory by avoiding memory used by application if it is not needed.

Lazy Rule

  1. You must always declare a lazy property as a variable (with the var keyword), because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.
  2. You can’t use lazy with let .
  3. You can’t use it with computed properties . Because, a computed property returns the value every time we try to access it after executing the code inside the computation block.
  4. You can use lazy only with members of struct and class .
  5. Lazy variables are not initailize atomically and so is not thread safe
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1