In order to store a class name in a log file I converted the description of a class type to a string:
let objectType: NSObject.Type = Object.self
let str = String(describing: objectType)
However, I do not succeed in back conversion of str
to a variable of type NSObject.Type
to use it in a generic method.
How could I do this?
You can get your class back from
string
, but you need to use your project's module name while getting class name. If you don't use your module name then it will returnnil
because the class name you have referenced earlier is not fully qualified by the module name. You should change the class name string to represent the fully qualified name of your class:I simply created an extension to use on any object:
The method classForCoder prints out the module name with the class name. Then you must that string in order to convert it back to its respective object type.
Perhaps something like: