5

Is the lazy attribute in Swift equivalent to overriding the getter with a lazy loading pattern in Objective C?

0

3 Answers 3

6

From the docs:

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 attribute before its declaration.

So, mostly, yes.

You must always declare a lazy property as a variable (with the var keyword), because its initial value may 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.”

Remember that on Swift you have the option to declare custom getters and setters for your properties:

var name : String?{
  get{
    return "Oscar"
  }
  set(newValue){

  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Would there be any benefits declaring a lazy pattern in the custom getter in Swift (if possible) over using the @Lazy attribute?
None that I have seen on this 24 hours of testing. That will probably change, though.
My pleasure. You can always select the answer as correct :-)
3

Referring to this:

lazy var x = SomeFunction()

The closest equivalent in Objective-C would be something like:

@property BOOL xHasBeenSet;
@property id x;

- (id)x
{
    if (!self.xHasBeenSet) {
        self.x = SomeFunction();
    }
    return x;
}

- (void)setX:(id)x
{
    _x = x;
    self.xHasBeenSet = YES;
}

Here you would only see SomeFunction called the first time you read x, but only if you don't set x first. It's important to note that there is only one code path where the right side gets called and it never resets back to xHasBeenSet = NO.

5 Comments

??? that would be .... weird. two properties to make one lazy? just do - (id)x { if(!_x) _x = someFunction()
@Daij-Djan For cases where SomeFunction() could return 0 or nil, the RHS would still only be called once.
ok -- then use a dispatch_once - (id)x { dispatch_once(^{ if(!_x) _x = boa; }); return _x; } [I left out the token because I always get the syntax wrong anyways :D]
self.x = ... was out of laziness since setting flipped two variables.
@Daij-Djan That misses the case where someone sets x = 0 before calling. It's tricky business. But yes, there's probably also a lock involved for thread safety.
2

mainly yes-- it can't be a computed property though

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.