Created
July 10, 2018 10:00
-
-
Save descorp/a0f2939320c96ed297b0ff59936c579c to your computer and use it in GitHub Desktop.
DTO printer
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 | |
import PortfolioSummaryClient | |
protocol ClassInfo : CustomDebugStringConvertible { | |
var className: String { get } | |
var properties: String { get } | |
} | |
extension NSObject : ClassInfo { | |
var className: String { | |
return NSStringFromClass(type(of: self)) | |
} | |
var properties: String { | |
return printOut(object: self, prefix: "") | |
} | |
var debugDescription: String { | |
return """ | |
\r\n | |
---- \(className) ---- | |
\(properties) | |
----------------- | |
""" | |
} | |
private func printOut(object: AnyObject, prefix: String) -> String { | |
var outCount : UInt32 = 0 | |
guard | |
let properties = class_copyPropertyList(type(of: object), &outCount) | |
else { | |
return self.description | |
} | |
var description = "" | |
for i in 0..<Int(outCount) { | |
guard | |
let label = String(cString: property_getName(properties[i]), encoding: String.Encoding.utf8), | |
let value = object.value(forKey: label), | |
label != "superclass", | |
label != "description", | |
label != "debugDescription", | |
label != "hash", | |
!label.contains("_") | |
else { continue }; | |
if let array = value as? [AnyObject] { | |
description += "\(prefix)\(label):\r" | |
var n = 0 | |
for item in array { | |
description += "\(prefix)[\(n)]:\r" | |
description += "\(prefix)\(printOut(object: item, prefix: prefix + "-"))\r" | |
n += 1 | |
} | |
} else if let item = value as? Additions { | |
description += "\(prefix)\(label):\r" | |
description += "\(prefix)\(printOut(object: item, prefix: prefix + "-"))\r" | |
} else { | |
description += "\(prefix)\(label): \(value)\r" | |
} | |
} | |
return description | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment