Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get class name of UIViewController in swift

How to get class name of UIViewController Class in swift

In Objective C, we can do something like this :

self.appDelegate = (shAppDelegate *)[[UIApplication sharedApplication] delegate];     UIViewController *last_screen = self.appDelegate.popScreens.lastObject ;      if(last_screen.class != self.navigationController.visibleViewController.class){      //.......  } 

but in swift I tried like this

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate     let last_screen = appDelegate.popScreens?.lastObject as UIViewController 

cant do this.

if last_screen.class != self.navigationController.visibleViewController.class{  //....  } 

no class method of UIViewController i.e last screen

like image 716
Qadir Hussain Avatar asked Oct 14 '14 10:10

Qadir Hussain


People also ask

Is UIViewController a subclass of Uiview?

Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy. A view controller's main responsibilities include the following: Updating the contents of the views, usually in response to changes to the underlying data.

What is UIViewController Swift?

A UIViewController is an object which manages the view hierarchy of the UIKit application. The UIViewController defines the shared behavior and properties for all types of ViewController that are used in the iOS application. The UIViewController class inherits the UIResponder class. ADVERTISEMENT. ADVERTISEMENT.

What is the difference between viewController and UIViewController?

An UIViewController is just the base class of this already defined "View Controller", you add a viewController as you said, but a viewController has a class associated to it, that can be a UIViewController, UITableViewController, or variations/subclasses.


2 Answers

To know your class name you can call something like this:

var className = NSStringFromClass(yourClass.classForCoder) 
like image 146
juangdelvalle Avatar answered Sep 21 '22 11:09

juangdelvalle


The cleanest way without needing to know the name of your class is like this.

let name = String(describing: type(of: self)) 
like image 20
cnotethegr8 Avatar answered Sep 23 '22 11:09

cnotethegr8