Skip to content

Instantly share code, notes, and snippets.

@garkhipov
Created March 31, 2015 10:28
Show Gist options
  • Save garkhipov/41a088998af07d66be6c to your computer and use it in GitHub Desktop.
Save garkhipov/41a088998af07d66be6c to your computer and use it in GitHub Desktop.
Optional chaining inside closure
// http://stackoverflow.com/questions/29365781/weird-behavious-using-optionals-inside-closure
struct GameSettings {}
protocol Delegate {
func didFinishSettings(settings: GameSettings, controller: Controller)
}
class Controller {
init() {}
var delegate: Delegate?
func setUpSettingsForNextGame(
successCompletion:(settings:GameSettings)->Void)
{}
func f2() {
setUpSettingsForNextGame {[weak self] (settings:GameSettings) in
if let delegate = self?.delegate {
delegate.didFinishSettings(settings, controller:self!)
}
}
}
func f1() {
setUpSettingsForNextGame {[weak self] (settings:GameSettings) in
self?.delegate?.didFinishSettings(settings, controller:self!)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment