Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Representing pointer values as emoji can be useful for "visually" debugging certain issues, like cell reuse, etc.
DEBUG: populating cell 0x00007fa2ec02e800 🏒
DEBUG: populating cell 0x00007fa2ec018a00 πŸŒ₯
DEBUG: populating cell 0x00007fa2eb005000 πŸŽ’
DEBUG: populating cell 0x00007fa2ec018a00 πŸŒ₯
DEBUG: populating cell 0x00007fa2ec02e800 🏒
DEBUG: populating cell 0x00007fa2ec032c00 🌩
DEBUG: populating cell 0x00007fa2ec030600 πŸ‚
DEBUG: populating cell 0x00007fa2ec033200 πŸŽƒ
DEBUG: populating cell 0x00007fa2e98b2000 🍜
DEBUG: populating cell 0x00007fa2e9815600 🍒
DEBUG: populating cell 0x00007fa2e98b3000 πŸ›
DEBUG: populating cell 0x00007fa2e98b3600 🎡
DEBUG: populating cell 0x00007fa2e9883000 πŸŽ‹
DEBUG: populating cell 0x00007fa2e9883600 πŸ₯
DEBUG: populating cell 0x00007fa2e9881000 🎍
DEBUG: populating cell 0x00007fa2e9883600 πŸ₯
DEBUG: populating cell 0x00007fa2e9883000 πŸŽ‹
DEBUG: populating cell 0x00007fa2e98b3600 🎡
DEBUG: populating cell 0x00007fa2e9815600 🍒
DEBUG: populating cell 0x00007fa2e98b3000 πŸ›
DEBUG: populating cell 0x00007fa2e98b2000 🍜
DEBUG: populating cell 0x00007fa2ec033200 πŸŽƒ
DEBUG: populating cell 0x00007fa2ec030600 πŸ‚
DEBUG: populating cell 0x00007fa2ec032c00 🌩
DEBUG: populating cell 0x00007fa2ec02e800 🏒
DEBUG: populating cell 0x00007fa2ec018a00 πŸŒ₯
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
window.makeKeyAndVisible()
window.rootViewController = ViewController()
self.window = window
return true
}
}
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "\(UITableViewCell.self)")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "\(UITableViewCell.self)", for: indexPath)
print("DEBUG: populating cell \(cell.asPointer) \(cell.asPointer.asEmoji)")
cell.textLabel?.text = "\(indexPath) \(cell.asPointer.asEmoji)"
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
}
extension NSObject {
public var asPointer: UnsafeMutableRawPointer {
return Unmanaged.passUnretained(self).toOpaque()
}
}
extension UnsafeMutableRawPointer {
public var asEmoji: String {
// Adapted from https://gist.github.com/iandundas/59303ab6fd443b5eec39
// Tweak the range to your liking.
//let range = 0x1F600...0x1F64F
let range = 0x1F300...0x1F3F0
let index = (self.hashValue % range.count)
let ord = range.lowerBound + index
guard let scalar = UnicodeScalar(ord) else {
return "❓"
}
return String(scalar)
}
}
@protspace
Copy link

cool

@pepasibble
Copy link

Update: here is a version which uses multiple ranges of emoji literals (emoji's aren't contiguously allotted):

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])
    }
}

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