Skip to content

Instantly share code, notes, and snippets.

@iandundas
Last active April 23, 2022 16:30
Show Gist options
  • Save iandundas/59303ab6fd443b5eec39 to your computer and use it in GitHub Desktop.
Save iandundas/59303ab6fd443b5eec39 to your computer and use it in GitHub Desktop.
Random Emoji
// Returns a random Emoji 🌿
extension String{
func randomEmoji()->String{
let range = 0x1F601...0x1F64F
let ascii = range.startIndex + Int(arc4random_uniform(UInt32(range.count)))
let emoji = String(UnicodeScalar(ascii))
return emoji
}
}
@cellularmitosis
Copy link

I adapted this for representing pointer addresses as emoji. Thanks! https://gist.github.com/cellularmitosis/d425aae5f1a2e5d9bfa1d4c1a5968d22

@kiler129
Copy link

Keep in mind this is NOT a definitive list of emojis: https://stackoverflow.com/questions/30470079/emoji-value-range

@pepasibble
Copy link

Heyo,

I've updated my approach to allow multiple discontiguous ranges of emojis. This allows you to make use of more emoji.

extension NSObject {
    public var asPointer: UnsafeMutableRawPointer {
        return Unmanaged.passUnretained(self).toOpaque()
    }
}


fileprivate let contiguousEmoji: [UnicodeScalar] = {
    let ranges: [ClosedRange<Int>] = [
        0x1f600...0x1f64f,
        0x1f680...0x1f6c5,
        0x1f6cb...0x1f6d2,
        0x1f6e0...0x1f6e5,
        0x1f6f3...0x1f6fa,
        0x1f7e0...0x1f7eb,
        0x1f90d...0x1f93a,
        0x1f93c...0x1f945,
        0x1f947...0x1f971,
        0x1f973...0x1f976,
        0x1f97a...0x1f9a2,
        0x1f9a5...0x1f9aa,
        0x1f9ae...0x1f9ca,
        0x1f9cd...0x1f9ff,
        0x1fa70...0x1fa73,
        0x1fa78...0x1fa7a,
        0x1fa80...0x1fa82,
        0x1fa90...0x1fa95,
    ]
    
    return ranges.reduce([], +).map { return UnicodeScalar($0)! }
}()


extension UnsafeMutableRawPointer {
    public var asEmoji: String {
        // Inspired by https://gist.github.com/iandundas/59303ab6fd443b5eec39
        let index = abs(self.hashValue) % contiguousEmoji.count
        return String(contiguousEmoji[index])
    }
}

usage:

foo.asPointer.asEmoji

https://gist.github.com/cellularmitosis/d425aae5f1a2e5d9bfa1d4c1a5968d22#gistcomment-3644352

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment