Swift ViewController class name as String
2 answers
There is probably a better way to do this, but as far as I know you can get the class name via classForCoder () (NSObject subclasses only). From there, you can use NSStringFromClass to convert the class name to NSString. The only problem is that the name is often rendered crippled, possibly "__lldb_expr_690.MyViewController". You can work around this by explicitly specifying the name, for example:
@objc(MyViewController) class MyViewController: UIViewController {
func aMethod() {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("Some Object", forKey: NSStringFromClass(self.classForCoder))
defaults.synchronize()
// Has set "Some Object" for key "MyViewController"
}
}
+3
source to share