Swift 4: type(of: self) differs when using private/fileprivate

大兔子大兔子 提交于 2019-12-24 14:43:16

问题


I implemented an extension to NSObject to get the dynamic type of my objects:

extension NSObject  {
    var dynamic_type : String {
        get {
            return String(describing: type(of: self))
        }
    }
}

This works perfectly for public classes.

In a class called InitialState dynamic_type would be "InitialState" (this is what I want) But as soon as I change the class to private or fileprivate it is something like "(InitialState in _AF5C6D4A3B423A6F0735A7740F802E5A)" (the parenthesis are also returned)

Why is this and what does the part after "in" mean? How can I get the plain class name for every type of class? (public, private, fileprivate)

I know that I could simply parse the string to get the plain class name but I want to understand what exactly happens and if I maybe am doing it wrong.

来源:https://stackoverflow.com/questions/48306011/swift-4-typeof-self-differs-when-using-private-fileprivate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!