Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing instance variables in then block #30

Closed
AndrewSB opened this issue Jul 20, 2016 · 3 comments
Closed

Accessing instance variables in then block #30

AndrewSB opened this issue Jul 20, 2016 · 3 comments

Comments

@AndrewSB
Copy link

I can't seem to access class variables in my then block:

For example

class GradientView {
   let colors = ...
   lazy var gradientLayer = CAGradientLayer().then {
       $0.colors = self.colors // also tried colors
   }
}

doesn't work, but

class GradientView {
   let colors = ...
   lazy var gradientLayer: CAGradientLayer = {
       let layer = CAGradientLayer()
       layer.colors = self.colors
       return layer
   }()
}

does.

Can I repeat my old pattern of accessing instance variables in a lazy init using Then?

@devxoul
Copy link
Owner

devxoul commented Aug 1, 2016

@AndrewSB, sorry for delay! It seems like a Swift bug. Adding type annotation to gradientLayer might work :)

class GradientView {
   let colors = ...
   lazy var gradientLayer: CAGradientLayer = CAGradientLayer().then {
       $0.colors = self.colors // also tried colors
   }
}

screen shot 2016-08-01 at 6 30 22 pm

@devxoul
Copy link
Owner

devxoul commented Sep 26, 2016

@AndrewSB, Doesn't it work?

@AndrewSB
Copy link
Author

It does! Thanks @devxoul!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants