Forked from YogevSitton-zz/AutoDescribingObjects.swift
Last active
June 26, 2018 08:41
-
-
Save asif-mistry/5f3dcd7b2e995a01e0cbe7afea299904 to your computer and use it in GitHub Desktop.
Use auto-describing objects with CustomStringConvertible
This file contains 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
extension CustomStringConvertible { | |
var description : String { | |
var description: String = "" | |
if self is AnyObject { | |
//description = "***** \(type(of: self)) - <\(unsafeAddressOf((self as! AnyObject)))>***** \n" | |
} else { | |
description = "***** \(type(of: self)) *****\n" | |
} | |
let selfMirror = Mirror(reflecting: self) | |
for child in selfMirror.children { | |
if let propertyName = child.label { | |
description += "\(propertyName): \(child.value)\n" | |
} | |
} | |
return description | |
} | |
} | |
class Dog : CustomStringConvertible { | |
let name: String | |
let age: Int | |
let color: String | |
init (name: String, age: Int, color: String) { | |
self.name = name | |
self.age = age | |
self.color = color | |
} | |
} | |
let bronoTheDog = Dog(name: "Brono", age: 4, color: "Brown") | |
let wendyTheDog = Dog(name: "Wendy", age: 2, color: "White") | |
print(bronoTheDog) | |
print(wendyTheDog) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment