Will casting "as?" fail if not the correct type?

I know this is a silly question, but I just want to be safe.

I have multiple subclasses of SKSpriteNode in my app. In touchesBegan I am looking for a specific type, MyPeg. I just want to make sure that the code below fails if the node is not a MyPeg. I have this fear that if another of my subclasses is the same size, or too similar, or even has the same variables that I might get a positive.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
        super.touchesBegan(touches , with:event)
        guard let touch = touches.first else { return }
        let location = touch.location(in: self)
        let touchedNodes = self.nodes(at: location)
        for theNode in  touchedNodes{
            if let node = theNode as? MyPeg{

Accepted Answer

Unless, theNode is of type MyPeg or is of any subclasses of MyPeg, theNode as? MyPeg will return nil and the if-statement will fail. No same size, being similar nor having the same variables do not effect.

As OOPer explained, it will fail.

It is not a question of size.

However, if you subclass MyPeg

class MySubPeg : MyPeg {
    var sub = "hello"
}

Then test will succeed.

Will casting "as?" fail if not the correct type?
 
 
Q