Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Lazy Properties in More Detail

The basic concept of lazy properties is as follows.

//  I create a function that performs a complex task.
func getDailyBonus() -> Int
{
    println("Performing a complex task and making a connection online")
    return random()  
}

//I set define a property to be lazy and only instantiate it when it is called.  I set the property equal to the function with the complex task 
class  Employee
{
    // Properties
    lazy var bonus = getDailyBonus()
}

I started working on a new project with CoreData and noticed that the Core Data stack is set up with Lazy Properties. However, the code is not something I've seen before and was hoping someone could help me understand the syntax.

// From Apple
lazy var managedObjectModel: NSManagedObjectModel =
{
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
    let modelURL = NSBundle.mainBundle().URLForResource("FLO_Cycling_1_1_1", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

Apple uses the { custom code }() syntax. My first thought was that they were simply using a closure when defining the lazy property to eliminate the need to create function first. Then I tried the following.

//  I tried to define a lazy property that was not an object like so. 
lazy var check = {
    println("This is your check")
}()

The compiler complained and suggested the following fix.

//  I do not understand the need for the ": ()" after check 
lazy var check: () = {
    println("This is your check")
}()

Can someone help me understand the syntax? The () at the end of the CoreData stack and the : () at the end of the check property is confusing me.

Take care,

Jon

like image 903
jonthornham Avatar asked Dec 30 '25 21:12

jonthornham


1 Answers

In the apple's example,

lazy var managedObjectModel: NSManagedObjectModel =
{
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
    let modelURL = NSBundle.mainBundle().URLForResource("FLO_Cycling_1_1_1", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

You can see that, apple implicitly specified the variable type:

lazy var managedObjectModel: NSManagedObjectModel

and returning a NSManagedObjectModel from that code itself.

In your first example, you are not specifying the type of that variable, also not returning any value (assigning or initializing). So the compiler complains that you need to specify a type implicitly and need to initialize it.

The basic idea is, you need to specify it's type implicitly and also you need to initialize it (writing just a println, doesn't initialize it. So in your scenario. You don't have any value for that particular lazy variable. So you need to specify it's type as a empty closure and you are initializing it with that.

Another example, The following will assign 7 to the check:

lazy var check : Int = {
    println("This is your check")
    return 7
}()
like image 96
Midhun MP Avatar answered Jan 01 '26 09:01

Midhun MP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!