Swift
Deferred loading of the mode
In Swift, you can implement this mechanism in one line of code:
Lazy var players = string[] ()
Simple, concise, straight into the topic.
But you have to remember that you have to use var
keywords to define deferred-loading properties, and you cannot use let
keywords, because constants must be assigned when the instance is built.
If you want to add some logic to lazy loading, Swift allows you to define a closure call after the property (the return value of the closure will be the default value for the property):
Lazy var players:string[] = { var temporaryplayers = string[] () temporaryplayers.append ("Mike Buss") Return temporaryplayers } ()
If you prefer, you can also use an instance method to initialize lazy-load properties:
Lazy var players:string[] = self.initialplayers () func initialplayers (), string[] { var players = ["Mike Buss"]
return Players}
Or you can use a class method:
Lazy var players = Multipeermanager.initialplayers () class Func initialplayers ()-string[] { var players = ["Mike Buss "] return players}
However, you are now more inclined to use the new closure syntax because it defines the logical code next to the attribute declaration.
When to use lazy loading?
One usage scenario is that the initial value of an object's property depends on its properties, so you must create the object before you know the value of the property.
For example, you have a Person
class and a personalizedGreeting
property. This personalizedGreeting
property needs to delay loading after the object is created, because only after the object is created does it know who the greeting is (person's name
). Please look at the code:
Class Person { var name:string lazy var personalizedgreeting:string = { [unowned Self] in return "Hello, \ (self.name)! " } () init (name:string) { self.name = name }}
Note that you must use [unowned self]
to avoid circular references. [unowned Self] defines a list of attributes/variables that need to be used in closures, which are outside the closure, also known as the Capture List ( capture list
).
When you instantiate one person
, his greeting greeting
is not created at this time:
Let person = person (name: "Robert Redford")//person.personalizedgreeting is nil
But when you try to print out a greeting, the greeting is automatically generated:
NSLog (person.personalizedgreeting)//personalizedgreeting is calculated then used//and now contains the value "Hello, Rob ERT redford! "
Another scenario that is suitable for lazy loading is when the initial value of a property requires a large amount of computation.
For example, when you have an object that needs to perform a high-load algorithm to determine the number of faces in a picture, you can numberOfFaces
set the property to lazy loading.
Or when you have a class that needs to calculate values for multiple large numbers, you want them to be calculated when needed:
Class Mathhelper { lazy var pi:double = { //Calculate pi to an insane number of digits return Resultofcalcul ation } ()}
Deferred loading in Swift (lazy loading)