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

前端 未结 7 811
遇见更好的自我
遇见更好的自我 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:31

    The reason you get "Bar.Foo" is because swift is a namespace language. I'm assuming the application you were running this in was named Bar. Hence you may get your class name using the following:

    let nameSpaceClassName = NSStringFromClass(Foo)
    let className = nameSpaceClassName.componentsSeparatedByString(".").last! as String
    
    0 讨论(0)
  • 2021-02-07 08:35

    Working solution for this in Swift 2.1 is

    String(MyClass)
    
    0 讨论(0)
  • 2021-02-07 08:39

    In objective-c your best option is:

    NSString *yourCellName = NSStringFromClass([yourCell class]).pathExtension;
    
    0 讨论(0)
  • 2021-02-07 08:40

    On the latest Swift below worked for me:

    NSStringFromClass(type(of: yourClass!.self))
    
    0 讨论(0)
  • 2021-02-07 08:46

    Try it with Swift 2.0:

    self.description().componentsSeparatedByString(".").last!
    
    0 讨论(0)
  • 2021-02-07 08:47

    Swift 5.1 solution for NSStringFromClass

    If you want the string description of a class name (say CustomTableViewCell) use:

    String(describing: CustomTableViewCell.self)
    
    0 讨论(0)
提交回复
热议问题