Forked from khramtsoff/CustomStringConvertible.swift
Last active
December 8, 2021 14:51
-
-
Save philosopherdog/36f1f03a04db612bda9890e38be1267e to your computer and use it in GitHub Desktop.
CustomStringConvertible prints memory address
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
import Foundation | |
extension CustomStringConvertible { | |
var description: String { | |
var description: String = "\(type(of: self))(" | |
let selfMirror = Mirror(reflecting: self) | |
for child in selfMirror.children { | |
if let propertyName = child.label { | |
description += "\(propertyName): \(child.value), " | |
} | |
} | |
description += "<\(Unmanaged.passUnretained(self as AnyObject).toOpaque())>)" | |
return description | |
} | |
} | |
class Person: CustomStringConvertible { | |
let name: String | |
let age: Int | |
init (name: String, age: Int) { | |
self.name = name | |
self.age = age | |
} | |
} | |
let alex = Person(name: "Alex", age: 20) | |
print(alex) // Person(name: Alex, age: 20, <0x000060c000058d20>) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment