I am trying to make a little MVP for the company I am working for (new job) and I am trying to dynamically load view controllers so I thought to add the following extension as listed below.
Only when I try to get the name of any class, e.g. UINavigationController it’s returning NSObject
for example in a snippet like:
let navController = UINavigationController()
Foundation.NSLog("LOG: Class Name: \(navController.nameOfClass)")
I am wondering what I am doing wrong? I would expect it would return whatever the class name is e.g. here UINavigationController
and when I would inherit from final class BlahVC: UIViewController etc
I would expect it to return BlahVC
.
The problem is that when I do something like:
extension UIViewController {
private class func instantiateControllerInStoryboard<T: UIViewController>(_ storyboard: UIStoryboard, identifier: String) -> T {
return storyboard.instantiateViewController(identifier: identifier) as! T
}
class func controllerInStoryboard(_ storyboard: UIStoryboard, identifier: String) -> Self {
return storyboard.instantiateViewController(identifier: identifier)
}
class func controllerInStoryboard(_ storyboard: UIStoryboard) -> Self {
return controllerInStoryboard(storyboard, identifier: self.nameOfClass)
}
class func controllerFromStoryboard(_ storyboard: Storyboards) -> Self {
return controllerInStoryboard(UIStoryboard(name: storyboard.rawValue, bundle: nil), identifier: self.nameOfClass)
}
}
public extension NSObject {
class public var nameOfClass: String {
return NSStringFromClass(self).componentsSeparated(by: ".").lastObject!
}
}