Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created February 15, 2023 16:50
Show Gist options
  • Select an option

  • Save foxicode/1b4d7bc8f8ee9f73957e6167cc9f7254 to your computer and use it in GitHub Desktop.

Select an option

Save foxicode/1b4d7bc8f8ee9f73957e6167cc9f7254 to your computer and use it in GitHub Desktop.
Protocols with static functions
import Foundation
protocol Hero {
static func weapon() -> String
static func fight()
}
extension Hero {
static func fight() {
print("Fighting with... \(weapon())")
}
}
class Archer: Hero {
static func weapon() -> String {
"bow with arrows"
}
}
class Knight: Hero {
static func weapon() -> String {
"sword"
}
}
func fightFunc(character: Hero.Type) {
character.fight()
}
let myCharacter = Archer()
type(of: myCharacter).self.fight()
fightFunc(character: type(of: myCharacter).self)
Archer.fight()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment