Lazy Stored Properties in swift ! How to work static and final in iOS | Static Property | Class Property | Final Property in iOS Swift 5.6





Lazy Stored Property in Swift

Hello friends, In the previous article we were talking about properties as computed and stored properties. Lazy is classified as stored attributes.

According to Apple's document, the simple definition

"Lazy property is stored property whose initial value is not calculated until the first time it used. You indicate a lazy stored property by writing the lazy modifier before its declaration". For example-


lazy var obj = ClassName()

👉You can’t use lazy with let .It must be of var type ! 
👉Lazy stored properties can be declared only in struct or class.
👉Property observers are not available to lazy stored properties.

When should you use lazy property?

👉Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete. 
👉Lazy properties are also useful when the initial value for a property requires complex or computationally expensive setup that should not be performed unless or until it is needed.

How should you use lazy property?

Lets begin start below example two classes without using lazy variable. As you can see, when I initialized User class, it also initialize ApplePhone class which has expensive task of allocating memory and compile the code.You can see the output.


Now, we include a lazy stored property by writing the lazy modifier before its declaration and notice that the ApplePhone class is not being initialized.

Type Properties-

Instance properties are properties that belong to an instance of a particular type. Every time you create a new instance of that type, it has its own set of property values, separate from any other instance.
You can also define properties that belong to the type itself, not to any one instance of that type. There will only ever be one copy of these properties, no matter how many instances of that type you create. These kinds of properties are called type properties.

👉Type Properties are used on the type(class/struct/enum) instead of the instance of that type.
👉Type Properties are defined with the keyword static.

Type property with Enum/Struct
Type property with class-
We define type properties engMarks and static method result() on an enum type Student with the keyword static. We can then access those type properties with the dot syntax on the type itself as in:   
print(Student.marks)  
Student.result()

What is static property?
We all know Instance Properties that belong to an instance of type and every time you create an instance of that type it has its own property value.

But what if you want to create a global property that all instances of the same type share? 
This is what we call Static or Type property because it belongs to the Type itself not an instance of it. 
There will be always one copy of Type property that all instance can use it.

When to use Static or class Property?
Use Static property when you don’t need to have different value of property for every instance. For example base url 
👉class keyword isn’t supported with stored properties.We can see the example of Student class.

👉Static Type Properties can’t be overridden in the subclass. The keyword class is used on the computed properties in this case.We can see the example of Child class.


What is the difference between static method and class method in Swift?
static and class both associate a method with a class, rather than an instance of a class. The difference is that subclasses can override class methods; they cannot override static methods.

👉Final cannot be overridden because that is the purpose of the keyword, something that cannot be changed or overridden.



You’ve done it!

We’ve covered the basics of Lazy and Type properties in this tutorial.. Read more on Apple Developer site. Subscribe to my account to get notifications about new stories.


If you enjoyed this article please share.


Thanks for reading!!


Related Posts:









Post a Comment

1 Comments

  1. Bets may be placed at as little as $0.10 in some instances, while excessive rollers are also catered for. With 50+ desk games in whole, Betwinner is a winner for fans of such games. We’ve already gone through out top 10 online casinos, but what is it about them that we like? To help you out additional, we’ve put collectively evaluations for each of the highest 5 South Korea online on line casino. With a execs and cons listing and a small evaluate for each site, you’ll know even more about which site is greatest for your online on line casino needs. Some highly-popular online slots with 카지노사이트 South Koreans are Vegas-style games like Sweet Bonanza or Starburst.

    ReplyDelete