devxoul / Then

✨ Super sweet syntactic sugar for Swift initializers
MIT License
4.18k stars 290 forks source link

Accessing instance variables in then block #30

Closed AndrewSB closed 7 years ago

AndrewSB commented 8 years ago

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 commented 8 years ago

@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 commented 7 years ago

@AndrewSB, Doesn't it work?

AndrewSB commented 7 years ago

It does! Thanks @devxoul!