Skip to content

Instantly share code, notes, and snippets.

@ericdke
Created May 22, 2015 18:31
Show Gist options
  • Save ericdke/c9aa9bccd160eb8afabf to your computer and use it in GitHub Desktop.
Save ericdke/c9aa9bccd160eb8afabf to your computer and use it in GitHub Desktop.
Transform a country acronym to an emoji flag
func flag(country: String) -> String {
let base = 127397 // Root Unicode flags index
var usv = String.UnicodeScalarView() // Prepare a Unicode string
for i in country.utf16 { // Loop over the country acronym letters
let inc = Int(i) // The letter's ASCII index
let code = UnicodeScalar(base + inc) // Shift the letter index to the flags index
usv.append(code) // Append the grapheme to the Unicode string
}
return String(usv)
}
let flagged = flag("FR") // prints "🇫🇷"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment