Skip to content

Instantly share code, notes, and snippets.

@godrm
Last active September 4, 2023 04:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save godrm/4a38e6005d9e77bb6c1291212ef82459 to your computer and use it in GitHub Desktop.
Save godrm/4a38e6005d9e77bb6c1291212ef82459 to your computer and use it in GitHub Desktop.
Swift Variable Address Dump to console
struct Memory {
static func dump<T>(variable: inout T) {
withUnsafePointer(to: &variable) { print($0) }
}
static func dump(with: UnsafeRawPointer) {
let address = Int(bitPattern: with)
print(String(format:"%p", address))
}
static func dump(object: AnyObject) {
print(Unmanaged.passUnretained(object).toOpaque())
}
}
var str1 = "abcd"
print("--------String(\(MemoryLayout.size(ofValue: str1)))-----")
Memory.dump(variable: &str1)
var array1 = [1,2,3]
print("--------Array(\(MemoryLayout.size(ofValue: array1)))------")
Memory.dump(variable: &array1)
Memory.dump(with: array1)
var value1 = 10
print("--------Int(\(MemoryLayout.size(ofValue: value1)))---------")
Memory.dump(variable: &value1)
class MyPoint {
var x : Int = 0
var y : Int = 0
}
let pp1 = MyPoint()
print("--------class(\(MemoryLayout.size(ofValue: pp1)))-------")
Memory.dump(object: pp1)
struct Point {
var x : Int
var y : Int
var dummy1 : Int = 1
var dummy2 : Int = 2
}
struct CardDeck {
private var deck = [Int]()
mutating func reset() {
deck.append(10)
deck.removeAll()
}
}
class MyPoint {
var x : Int = 0
var y : Int = 0
}
var ptr : MyPoint?
func foo() {
var str1 = "abcd"
print("--------String(\(MemoryLayout.size(ofValue: str1)))-----")
Memory.dump(variable: &str1)
Memory.dump(with: str1)
var str2 = str1
Memory.dump(variable: &str2)
Memory.dump(with: str2)
str2 = "xxxx"
Memory.dump(variable: &str2)
Memory.dump(with: str2)
var array1 : Array<Int> = [1,2,3]
print("--------Array(\(MemoryLayout.size(ofValue: array1)))------")
Memory.dump(variable: &array1)
Memory.dump(with: array1)
var array2 = array1
Memory.dump(variable: &array2)
Memory.dump(with: array2)
array1.append(4)
Memory.dump(variable: &array1)
Memory.dump(with: array1)
var value1 = 10
print("--------Int(\(MemoryLayout.size(ofValue: value1)))---------")
Memory.dump(variable: &value1)
var value2 = value1
Memory.dump(variable: &value2)
value1 = 11
Memory.dump(variable: &value1)
var ptr1 = Point(x: 1, y: 1)
print("--------Point(\(MemoryLayout.size(ofValue: ptr1)))------")
Memory.dump(variable: &ptr1)
var ptr2 = ptr1
Memory.dump(variable: &ptr2)
ptr1.x = 10
Memory.dump(variable: &ptr1)
var pp1 = MyPoint()
pp1.x = 10
pp1.y = 20
print("--------class(\(MemoryLayout.size(ofValue: pp1)))-------")
Memory.dump(with: &pp1)
Memory.dump(object: pp1)
var pp2 = pp1
Memory.dump(with: &pp2)
Memory.dump(object: pp2)
ptr = pp2
print("--------CardDeck-------")
var deck = CardDeck()
Memory.dump(variable: &deck)
deck.reset()
Memory.dump(variable: &deck)
}
func bar() {
enum Fruit {
case apple
case orange
}
let character = Fruit.apple
print("bar-\(character)")
}
foo()
bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment