Skip to content

Instantly share code, notes, and snippets.

@noa4021J
Last active January 3, 2022 11:44
Show Gist options
  • Save noa4021J/a423867eb81ba8378e4f131e60a54344 to your computer and use it in GitHub Desktop.
Save noa4021J/a423867eb81ba8378e4f131e60a54344 to your computer and use it in GitHub Desktop.
Open Closed Principle in Swift
enum Fruit {
case apple
case orange
case banana
case grape
}
func functionA(_ fruit: Fruit) {
switch fruit {
case .apple:
// do something
case .orange:
// do something
case .banana:
// do something
case .grape:
// do something
}
}
func functionB(_ fruit: Fruit) {
switch fruit {
case .apple:
// do something
default:
// break or do default something
}
}
func functionC(_ fruit: Fruit) {
if case .apple = fruit {
// do something
} else {
// do something
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment