Skip to content

Instantly share code, notes, and snippets.

@ksmandersen
Created June 19, 2016 08:52
Show Gist options
  • Save ksmandersen/dd849faa99fffeded2e46cd393e0c65d to your computer and use it in GitHub Desktop.
Save ksmandersen/dd849faa99fffeded2e46cd393e0c65d to your computer and use it in GitHub Desktop.
class AwesomeView: GenericView {
let bestTitleLabel = UILabel().then {
$0.textAlignment = .Center
$0.textColor = .purpleColor()tww
}
let otherTitleLabel = UILabel().then {
$0.textAlignment = .
$0.textColor = .greenColor()
}
override func configureView() {
super.configureView()
addSubview(bestTitleLabel)
addSubview(otherTitleLabel)
// Configure constraints
}
}
@ksmandersen
Copy link
Author

@mrgrauel I know that is the Objective-C and storyboard style to keep references to views weak, and with good reasons to avoid retain-cycles. But I actually do prefer to keep the references strong. I have had places where views are not added at initialization or I want to remove and add them again. And as long as you're not doing anything weird with how you add the views into the view hierarchy you don't get retain cycles. Make the views weak only makes them more annoying to deal with when referencing them later.

@ryanwilson: I wasn't aware of this technique. It's quite clever. I do still prefer the then syntax, but it's nice to know that it can be achieved without 3rd party code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment