Last active
January 3, 2022 11:44
-
-
Save noa4021J/a423867eb81ba8378e4f131e60a54344 to your computer and use it in GitHub Desktop.
Open Closed Principle in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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