Skip to content

Instantly share code, notes, and snippets.

@ireeymonse
Last active June 9, 2020 16:51
Show Gist options
  • Save ireeymonse/122d1c06f651b36212153bc4e1ac5720 to your computer and use it in GitHub Desktop.
Save ireeymonse/122d1c06f651b36212153bc4e1ac5720 to your computer and use it in GitHub Desktop.
Generate an emoji using an object's memory address.
import Foundation
// 1056 emoji in the range
let emoji: [UnicodeScalar] = (0x1F300...0x1F9FF).compactMap {
if let scalar = UnicodeScalar($0), scalar.properties.isEmoji { // see also: isEmojiPresentation
return scalar
}
return nil
}
/// Generates an emoji using the object's memory address.
func emoji<T: AnyObject>(from object: T) -> String {
let address = Int(bitPattern: Unmanaged.passUnretained(object).toOpaque())
return String(emoji[address % emoji.count])
}
// sample object: <UIViewController: 0x7ff4ffc.....>
// output: 🍫
/* first attempt
func emoji<T: AnyObject>(from x: T) -> String {
let address = Int(bitPattern: Unmanaged.passUnretained(x).toOpaque())
if let scalar = UnicodeScalar(0x1F300 + address % (0x1F9FF - 0x1F300)) {
return String(scalar)
}
return ""
} */
// from the the range, 1044 are real emoji, the rest will show as [?]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment