Skip to content

Instantly share code, notes, and snippets.

@matsuda
Last active March 14, 2024 09:41
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • 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