I would like to have a class name with a module prefix like we had with NSStringFromClass
.
The problem of NSStringFromClass
is that it mangles some classes names. So the first approach would be to use type(of:)
but that doesnt has the module name. (I need the module name for backward compatibility). So far I came with the following:
func typeName(value: Any) -> String {
let valueType = type(of: value)
var typeName = String(describing: valueType)
//warning explanation: https://forums.swift.org/t/any-as-anyobject/11182
if valueType is AnyClass, let obj = value as? AnyObject {
let typeObject : AnyObject.Type = type(of: obj)
let objcTypeName = NSStringFromClass(typeObject)
if objcTypeName.contains(substring: ".") {
if let moduleName = objcTypeName.components(separatedBy: ".").first {
//we need to prefix the module name to the class name
typeName = moduleName + "." + typeName
}
}
}
return typeName
}
Any chance somebody can suggest a better approach with the current constrains I have?