Calling NSStringFromClass on a Swift Class in Objective-C returns module mangled name

前端 未结 7 775
遇见更好的自我
遇见更好的自我 2021-02-07 07:53

I am aware of this question regarding how we can get a readable class name of an objective-c class in Swift.

What I want to achieve is getting the readable class name of

相关标签:
7条回答
  • 2021-02-07 08:54

    BEFORE SWIFT 2.1:

    Just put @objc(YourClassName) in your swift class:

    @objc(YourClassName)
    
    class YourClassName: NSObject {
    
    }
    

    And you can use NSStringFromClass like this:

    NSStringFromClass(YourClassName.self)
    

    It should also work from Objective-C then.


    SWIFT 2.1

    With Swift 2.1 a comment to this answer states that this is sufficient:

    class YourClassName: NSObject {
    
    }
    

    And just use:

    var str = String(YourClassName)
    

    I have not tested this from Objective-C code myself though.


    There's been a edit-suggestions that want to use this instead for Swift 4:

    var str = String(describing: YourClassName.self)
    

    I've not tested this from Objective-C though.

    0 讨论(0)
提交回复
热议问题