First off all: sorry for the title, but I didn't now how to better formulate the meaning of my following question in a single phrase.
While I was writing the following Swift
code:
if errorData.isMemberOfClass(UIAlertController) {
self.presentViewController(errorData, animated: true, completion: nil)
} else {
/* Some other code */
}
for my personal project the compiler gave me this error:
'AnyObject' is not convertible to 'UIAlertController'; did you mean to use 'as!' to force downcast?
and it won't let me compiler any further if I would not change my code in to this:
if errorData.isMemberOfClass(UIAlertController) {
self.presentViewController(errorData as! UIAlertController, animated: true, completion: nil)
} else {
/* Some other code */
}
But I don't get this: if I make the program check the class of errorData
(which, to be precise, is of class AnyObject
) using the if statement
why does it force me to force downcast against the type of class is supposed to be inside the if
?
In other words: doesn't the compiler already knows that inside the statement errorData is UIAlertController
? If it wasn't the program continued in the else portion instead, so errorData could even be of class Int
or String
for example