Reputation: 171
I just want to convert any UIViewController
subclass to AnyClass
for checking from the isKindOfClass
method because this method takes AnyClass
as argument.
Upvotes: 3
Views: 4457
Reputation: 171
Finally I reached at this solution
let name = NSStringFromClass((vcAnyObj?.classForCoder)!)
let name2 = NSStringFromClass((appDelegateObj.navigationController!.topViewController?.classForCoder)!)
if name == name2 {
print("same view controller")
}else {
print("different view controller")
}
Upvotes: 0
Reputation: 5341
func someMethod (someClass : AnyClass) {
if someClass is YourClass {
someClass.someMethodOfYourClass()
}
}
To use this method
class YouClass: UIViewController {
func something() {
someMethod(self) // sending itself (a.k.a) YourClass
}
}
Hope this helps
Edit based on comment :
let controller = appDelegateObj.navigationController!.topViewController
if controller is ClassName {
// controller good to use
}
Upvotes: 1
Reputation: 4859
Instead of using old Objective-C
API, you can use the as
or is
keyword in swift.
if let controller = yourViewController as? SomeClass {
// use controller, is already casted to SomeClass
}
if yourViewController is SomeClass {
let controller = yourViewController as! SomeClass
// use controller now
}
ADDING:
If the memory addresses are different, the objects are not equal. Try to check for the topViewController
type as I mentioned in the answer.
if let topViewController = appDelegateObj.navigationController.topViewController as? YOUR_VIEW_CONTROLLER_CLASS {
// do something
}
Upvotes: 4