NSStringFromClass in Swift is Here!

One of the first things I immediately noticed was missing from Swift was the NSStringFromClass replacement. I like to use the class name for my TableViewCell identifiers, and then just NSStringFromClass to dequeue the cells to avoid spelling issues.

In Swift, I would write an ugly extension to do that as seen in this StackOverflow answer:

Not sure when it happened, but I was extremely to see a more native solution in @aligatr’s blog post!

I tried it out myself just to be sure and it actually works!!

custom BlueTableViewCell

Custom BlueTableViewCell

Super excited I don’t have to use the extension anymore!

Enjoy the article? Join over 14,500+ Swift developers and enthusiasts who get my weekly updates.

  • Allan Bto

    Hey Natasha,

    Great idea! I’m also using it for my UITableViews, I wrote this little extension to help me:

  • Whats the difference between String(self) and String(reflecting: self)?

  • Limon

    I use it.

    extension UICollectionReusableView {

    static var reuseIdentifier: String {

    return "(self)"



  • IanKay

    Nice: I was able to simplify this


  • Juan Collas

    Under Xcode 7.1, I’ve started using this:


    or in a protocol,


    I found this somewhere in stack overflow, I’m sure.

  • KenZhang9999

    This library does a great help on identifiers of cells, segues, resources. You can use automatically generated constants instead of strings.

  • fabb

    The Swift variant does not include the module name though, while the ObjC variant does. Might lead to name clashes in the very rare case that same-named reusable views from different modules are used within the same collection/table view.

  • Eduardo Milpax DĂ­az

    This does not work on Swift 3

    • Ilya Denisov

      Yep, for swift 3
      let className = String(describing: SomeClass.self)