Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Created September 25, 2016 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/a373a7d68e6808c06d6760d2a834f488 to your computer and use it in GitHub Desktop.
Save krzyzanowskim/a373a7d68e6808c06d6760d2a834f488 to your computer and use it in GitHub Desktop.
Override Swift function by extension
// hm... override function implementation. This may be unexpected.
public extension String {
// wait wait wait... why no error here?
// String already have `public func hasPrefix(_ prefix: String) -> Bool` in stdlib
// https://github.com/apple/swift/blob/caa173b27a34a26f82a15ccc76cf4d8f95587637/stdlib/public/core/StringLegacy.swift#L143
public func hasPrefix(_ prefix: String) -> Bool {
return true // fake it till you make it
}
}
let s = "abcd"
if s.hasPrefix("oooo") {
print("hasPrefix true")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment