Swift turn a country code into a emoji flag via unicode

后端 未结 7 896
日久生厌
日久生厌 2020-12-07 14:08

I\'m looking for a quick way to turn something like:

let germany = \"DE\" 

into

let flag = \"\\u{1f1e9}\\u{1f1ea}\"
         


        
7条回答
  •  渐次进展
    2020-12-07 14:19

    Two optimizations of matt's answer.

    • No need to pass through the nested String in Swift 4
    • To avoid pass lower case string, I added uppercased()

    Here is the code.

    func flag(from country:String) -> String {
        let base : UInt32 = 127397
        var s = ""
        for v in country.uppercased().unicodeScalars {
            s.unicodeScalars.append(UnicodeScalar(base + v.value)!)
        }
        return s
    }
    

提交回复
热议问题