In Objective-C, I would use the following code to identify an instance of a class and the function being called in the console.
NSLog(@"[DMLOG - %@] %@", NSStringFromSelector(_cmd), self);
This would return something like the console out below, where I would get an instance ID to track different instances of an object.
[DMLOG - prepForInput] <GridBase: 0x7fb71860a190>
How can I get both the instance ID and the function being called within Swift? I've tried the following to get the ID in Swift but it only provides the class name and no instance ID value? Any suggestions would be appreciated.
print("[DBG] Init: \(self)")
To get current function name use #function
literal.
As for instance ID, looks like you have two options:
Inherit from NSObject
(UIKit classes inherit it anyways) so that class instances would have instance IDs:
class MyClass: NSObject {
override init() {
super.init()
print("[DBG: \(#function)]: \(self)")
}
}
If your class does not inherit from NSObject
you can still identify your instances by memory address:
class Jumbo {
init() {
print("[DBG: \(#function)]: \(Unmanaged.passUnretained(self).toOpaque())")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With