Skip to content

Instantly share code, notes, and snippets.

@dagronf
Created March 2, 2020 22:36
Show Gist options
  • Save dagronf/6608307464ba5fb624ff9491b87db0e1 to your computer and use it in GitHub Desktop.
Save dagronf/6608307464ba5fb624ff9491b87db0e1 to your computer and use it in GitHub Desktop.
SWIFT: Stream generic functions for NSView and NSCell to allow quick initialisation of properties
/// Stream generic functions for NSView and NSCell to allow quick initialisation of properties
/// Useful for when your (eg) button variable has a long name or is optional.
func <<<T> (left: T, configureBlock: (T) -> Void) -> T where T: NSView {
configureBlock(left)
return left
}
func <<<T> (left: T, configureBlock: (T) -> Void) -> T where T: NSCell {
configureBlock(left)
return left
}
/// Usage:
/// self.performTaskButton = NSButton() << {
/// $0.setButtonType(.toggle)
/// $0.title = "Button!"
/// $0.alternativeTitle = "button pressed"
/// $0.setContentHuggingPriority(.required, for: .horizontal)
/// $0.setContentHuggingPriority(.required, for: .vertical)
/// $0.setContentCompressionResistancePriority(.required, for: .horizontal)
/// $0.setContentCompressionResistancePriority(.required, for: .vertical)
/// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment