Skip to content

Instantly share code, notes, and snippets.

@yycking
Last active November 14, 2019 07:17
Show Gist options
  • Save yycking/b09c7c4dbcae2ba4a6b9e642a9c7f5e3 to your computer and use it in GitHub Desktop.
Save yycking/b09c7c4dbcae2ba4a6b9e642a9c7f5e3 to your computer and use it in GitHub Desktop.
convert country code to emoji flag
extension Locale {
var flagCode: String {
guard let country = self.regionCode else {return "🌐"}
let base : UInt32 = 127397
let code = country.unicodeScalars.compactMap{UnicodeScalar(base + $0.value)}
return String(String.UnicodeScalarView(code))
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
Text(Locale.current.flagCode)
.font(.system(size: 60))
}
}
import PlaygroundSupport
PlaygroundPage.current.setLiveView(ContentView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment