Skip to content

Instantly share code, notes, and snippets.

@vojto
Created March 8, 2017 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vojto/7ddbb2ce545414469d49d4dfccc6a8e8 to your computer and use it in GitHub Desktop.
Save vojto/7ddbb2ce545414469d49d4dfccc6a8e8 to your computer and use it in GitHub Desktop.
class RectangleConfig {
var width: MutableProperty<Int>(value: 0)
var height: MutableProperty<Int>(value: 0)
}
class ConfigView: NSView {
// Models
let config = MutableProperty<RectangleConfig?>(nil)
var width: SignalProducer<Int?, NoError>!
func awakeFromNib() {
// Extract config.width to self.width
// self.width should change when config.width changes, or when
// config changes
// self.width is Optional<Int> because config is Optional<RectangleConfig>
// and so if there is no config, then there is no width
self.width = config.producer.flatMap(.latest) { maybeConfig -> SignalProducer<Int?, NoError> in
if let config = maybeConfig {
return config.width.producer
} else {
return SignalProducer<Int?, NoError>(value: nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment