Skip to content

Instantly share code, notes, and snippets.

@jormungand
Created December 14, 2018 17:28
Show Gist options
  • Save jormungand/5f5a0b1eb69b97d9c1e5b43730c90ca1 to your computer and use it in GitHub Desktop.
Save jormungand/5f5a0b1eb69b97d9c1e5b43730c90ca1 to your computer and use it in GitHub Desktop.
Printing <type: address> of literally any instance (objective-c and swift)
struct Pointer<T: Any>: CustomStringConvertible {
let address: UInt
let description: String
let debugDescription: String
init(to valueTypePtr: UnsafePointer<T>) {
address = UInt(bitPattern: valueTypePtr)
let pointee = valueTypePtr.pointee
description = "<\(type(of: pointee)): 0x\(address.hexString)>"
debugDescription = "\(description) == \( String(describing: pointee) )"
}
}
////////////////////////
extension Pointer where T: AnyObject {
init(to object: T) {
address = UInt(bitPattern: ObjectIdentifier(object))
description = "<\(type(of: object)): 0x\(address.hexString)>"
debugDescription = String(describing: object)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
extension FixedWidthInteger {
var hexString: String {
return String(self, radix: 16, uppercase: false)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
func testThis(){
var aa = CGSize.zero
print(Pointer(to: &aa)) // <CGSize: 0x7ffeec957c78>
let bb = UILabel(frame: .zero)
print(Pointer(to: bb)) // <UILabel: 0x7f90e9504330>
var cc: CGSize? = nil
print(Pointer(to: &cc)) // <Optional<CGSize>: 0x7ffeec957c60>
var dd: NSObject? = nil
print(Pointer(to: &dd)) // <Optional<NSObject>: 0x7ffeec957cb8>
var ee1 = UIStatusBarStyle.lightContent
print(Pointer(to: &ee1)) // <UIStatusBarStyle: 0x7ffeecea6bd0>
var ee2 = UIStatusBarStyle.lightContent
print(Pointer(to: &ee2)) // <UIStatusBarStyle: 0x7ffeecea6bd8>
var ff = 123
print(Pointer(to: &ff)) // <Int: 0x7ffee27b3c58>
var gg = "hello"
print(Pointer(to: &gg)) // <String: 0x7ffee86adc28>
var hh: (Int) -> String? = { return "\($0)" }
print(Pointer(to: &hh)) // <(Int) -> Optional<String>: 0x7ffee86adba8>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment