Skip to content

Instantly share code, notes, and snippets.

@GUIEEN
Forked from matsuda/memoryAddress.swift
Created March 16, 2021 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GUIEEN/bb4ecffad3d131c3cdd5de5b755a8389 to your computer and use it in GitHub Desktop.
Save GUIEEN/bb4ecffad3d131c3cdd5de5b755a8389 to your computer and use it in GitHub Desktop.
Get memory address in Swift
///
/// https://stackoverflow.com/a/29741007
///
let s = Struct() // Struct
withUnsafePointer(to: s) {
print(String(format: "%p", $0)
}
///
/// http://stackoverflow.com/a/36539213/226791
///
func addressOf(_ o: UnsafeRawPointer) -> String {
let addr = unsafeBitCast(o, to: Int.self)
return String(format: "%p", addr)
}
func addressOf<T: AnyObject>(_ o: T) -> String {
let addr = unsafeBitCast(o, to: Int.self)
return String(format: "%p", addr)
}
///
/// 強制的にメモリーワーニングを発生させる(実機)
///
let selector = Selector("_performMemoryWarning")
if UIApplication.shared.responds(to: selector) {
UIApplication.shared.perform(selector)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment