Last active
August 26, 2024 04:47
-
-
Save godrm/4a38e6005d9e77bb6c1291212ef82459 to your computer and use it in GitHub Desktop.
Swift Variable Address Dump to console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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