Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xiaoxidong/6bf1df63cb4e37ee6c9e2c0760707a6b to your computer and use it in GitHub Desktop.
Save xiaoxidong/6bf1df63cb4e37ee6c9e2c0760707a6b to your computer and use it in GitHub Desktop.
Emoji grouping
JSON:
https://unpkg.com/[email protected]/emoji.json
class EmojiManager: ObservableObject {
@Published var emojis: [EmojiObject] = []
init() {
decodeJSON()
}
func decodeJSON() {
if let url = Bundle.main.url(forResource: "emoji", withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let emoji: [EmojiObject] = try! JSONDecoder().decode([EmojiObject].self, from: data)
self.emojis = emoji
} catch {
print("error:\(error)")
}
}
}
}
struct EmojiObject: Decodable, Hashable, Identifiable {
let codes, char, name, category: String
let subgroup: String
let group: EmojiGroup
var id: String { codes }
}
enum EmojiGroup: String, CaseIterable, Codable, Identifiable {
case smileys = "Smileys & Emotion"
case people = "People & Body"
case component = "Component"
case animals = "Animals & Nature"
case foodAndDrink = "Food & Drink"
case travel = "Travel & Places"
case activities = "Activities"
case objects = "Objects"
case symbols = "Symbols"
case flags = "Flags"
var id: String { rawValue }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment