1

There's this AViewController which is written in Swift. Now in my Obj C manager class I want to check the type of the viewcontroller. But it doesn't read because Obj C read the class name as "ProjectName.AViewController". How can we have the below mentioned check?

//Swift class
class AViewController: UIViewController {
}

//Obj C - Manager class
- (void)sampleMethod:(UIViewController *)viewController {
 if ([viewController isKindOfClass:[AViewController class]]) {
  //Action
 }
}
Kumar Nitin
  • 1,845
  • 2
  • 16
  • 21
  • 2
    Is `isKindOfClass:` actually failing here? The actual name of the class shouldn't matter, since you're using `[AViewController class]` to check it. the above should work. In my tests, it does. – Rob Napier Apr 08 '15 at 13:00
  • @RobNapier Yes, and that is because [AViewController class] is treated as class of type AViewController, where as variable viewController is treated as class of type ProjectName.AViewController – Kumar Nitin Apr 08 '15 at 15:28

4 Answers4

2

Add @objc(...Name...) before the class name in Swift.

Eg: Swift

//Swift class
@objc(SwiftAViewController)
class AViewController: UIViewController {
}

Eg: ObjC

//Obj C - Manager class
- (void)sampleMethod:(UIViewController *)viewController {
 if ([viewController isKindOfClass:[SwiftAViewController class]]) {
  //Action
 }
}

More details: Name of Swift class when exposed to Objective-C code does not respect the @objc renaming

Community
  • 1
  • 1
Kyle Howells
  • 3,008
  • 1
  • 25
  • 35
0

Because of the bug in swift, @Freerunnering answer will not work. As a work around, we can get the class name as string and then do the string match.

if ([NSStringFromClass([AViewController class] isEqualToString:@"ProjectName.AViewController"]) {

}

Kumar Nitin
  • 1,845
  • 2
  • 16
  • 21
  • 1
    Can you explain a bit more about that bug? – Miknash Apr 08 '15 at 14:22
  • Sure, the compiler couldn't read [viewController isKindOfClass:[SwiftAViewController class]] and hence it'll not compile. I tried cleaning the project and derived data but it wasn't was much help. – Kumar Nitin Apr 08 '15 at 15:18
  • @NickCatib I will have to do some research on that. It looks like a bug to me. Read this http://stackoverflow.com/questions/26132823/name-of-swift-class-when-exposed-to-objective-c-code-does-not-respect-the-objc?lq=1 – Kumar Nitin Apr 08 '15 at 15:31
-1
let arrControllers = self.navigationController.viewControllers

                    for (indx, vc) in (arrControllers?.enumerated())! {
                        if vc is YourViewController{
                            self.navigationController.viewControllers.remove(at: indx)
                        }
                    }
Pritesh
  • 980
  • 7
  • 17
-1

you can use below code:

for viewController in viewControllers {
                            if viewController.isKind(of: OurViewController.self){
                                print("yes it is OurViewController")
                                self.navigationController?.popToViewController(viewController, animated: true)
                            }
                        }

for more detail you can refer this answer already answered question

SARATH SASI
  • 1,395
  • 1
  • 15
  • 39