Skip to content

Instantly share code, notes, and snippets.

@TaimurAyaz
Forked from Kametrixom/emoji.swift
Created November 21, 2016 07:43
Show Gist options
  • Save TaimurAyaz/c990e6f1d29329796d84c21f5781f64b to your computer and use it in GitHub Desktop.
Save TaimurAyaz/c990e6f1d29329796d84c21f5781f64b to your computer and use it in GitHub Desktop.
Gets all Emoji Unicode code points from the latest Unicode source and prints them out (as Emoji). Also: Prints out all country flag emojis from "AA" to "ZZ"
import Foundation
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely()
func getAllEmojis(completion: String? -> ()) {
let url = NSURL(string: "http://www.unicode.org/Public/UCD/latest/ucd/EmojiSources.txt")!
NSURLSession.sharedSession().dataTaskWithURL(url) { data, response, error in
guard let data = data where error == nil else {
completion(nil)
return
}
let string = NSString(data: data, encoding: NSASCIIStringEncoding) as! String
let lines = string.componentsSeparatedByString("\n")
var view = String.UnicodeScalarView()
for line in lines where !line.hasPrefix("#") {
let chars = line.characters
guard let semicolonIndex = chars.indexOf(";") else { continue }
let codePoints = String(chars[chars.startIndex..<semicolonIndex])
.componentsSeparatedByString(" ")
.flatMap { UInt32($0, radix: 16) }
.map ( UnicodeScalar.init )
view.extend(codePoints)
}
completion(String(view))
}.resume()
}
getAllEmojis { print($0) }
func toCountryEmoji(string: String) -> String? {
guard string.characters.count == 2 &&
!string.characters.contains({ !("A"..."Z" ~= $0) }) else { return nil }
var view = String.UnicodeScalarView()
view.extend(string.unicodeScalars.map { UnicodeScalar($0.value + 127397) })
return String(view)
}
let alphabet = (0..<26).map { String(UnicodeScalar($0 + 65)) }
print(alphabet.reduce("\t") { $0 + $1 + "\t" }, appendNewline: false)
for fst in alphabet {
print("\n\(fst)\t", appendNewline: false)
for snd in alphabet {
print("\(toCountryEmoji(fst + snd)!)\t", appendNewline: false)
}
}
print("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment