Skip to content

Instantly share code, notes, and snippets.

@streeter
Forked from cellularmitosis/EmojiPointersDemo.swift
Created September 19, 2018 17:13
Show Gist options
  • Save streeter/44454fec7c017e8b3c65cfa191d4adb6 to your computer and use it in GitHub Desktop.
Save streeter/44454fec7c017e8b3c65cfa191d4adb6 to your computer and use it in GitHub Desktop.
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment