Okay. Attempted to use your code as follows (including only changed / added code):
extension UIApplication {
...
while true {
if let presented = topViewController?.presentedViewController {
topViewController = presented
...
} else if let cptVC = topViewController as? CPTViewController {
topViewController = cptVC
} else {
break
}
}
return topViewController
}
}
// Used like this:
let cptVC = UIApplication.shared.topViewController()
...
(cptVC as! CPTViewController).hidePOI(). // Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Don't know what I'm doing wrong, but it feels like everything else in Swift - like I'm having to cluge things together until there are no indicated errors, but most of the time don't work anyway. I feel like it's my lack of knowledge somehow, but I don't know what I'm missing.
Main questions:
Why doesn't the declaration of cptVC (in the 'Used like this' section) carry the CPTViewController type from the extension?
I understand why cptVC has to be unwrapped (its an optional result), but I don't understand why it has to be type-cast when used.
Bah!