虽然不像Objective-C块语法那么难以记住,但这并不明显.在此示例中,notConnected参数是可选的:
func whenConnected(block: Void -> Void,notConnected: ((Void) -> Void)?,showErrorMessage: Bool) -> Void { let connected = Reachability.isConnectedToNetwork() if connected { block() } else { notConnected?() } if showErrorMessage { // your error handling // } }