Skip to content

Instantly share code, notes, and snippets.

@matsuda
Last active October 10, 2024 04:39
Show Gist options
  • Save matsuda/0dbb9721a9ee8cddb8b09d885c8f4290 to your computer and use it in GitHub Desktop.
Save matsuda/0dbb9721a9ee8cddb8b09d885c8f4290 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