Search code examples
iosswiftactionscript-3

Swift equivalent of this ActionScript function


I translated this tutorial on BSP into swift. In the tutorial there's this ActionScript function.

public function getRoom():Rectangle
{
    // iterate all the way through these leafs to find a room, if one exists.
    if (room != null)
        return room;
    else
    {
        var lRoom:Rectangle;
        var rRoom:Rectangle;
        if (leftChild != null)
        {
            lRoom = leftChild.getRoom();
        }
        if (rightChild != null)
        {
            rRoom = rightChild.getRoom();
        }
        if (lRoom == null && rRoom == null)
            return null;
        else if (rRoom == null)
            return lRoom;
        else if (lRoom == null)
            return rRoom;
        else if (FlxG.random() > .5)
            return lRoom;
        else
            return rRoom;
    }
}

I translated this function into Swift (to the best of my ability) I must have written it wrong because the function is returning a nil value when it shouldn't be.

My version in Swift:

// left/right child are initialized as follows:
// leftChild:Room?
// rightChild:Room?

public func getRoom() -> Room? {
    if room != nil {
        return room
    } else {
        var lRoom:Room?
        var rRoom:Room?

        if leftChild != nil {
            lRoom = leftChild!.getRoom()!
        }
        if rightChild != nil {
            rRoom = rightChild!.getRoom()!
        }
        if lRoom == nil && rRoom == nil {
            return nil
        } else if rRoom == nil {
            return lRoom
        } else if lRoom == nil {
            return rRoom
        } else if  Double.random(in: 0..<1.0) > 0.5 {
            return lRoom
        } else {
            return rRoom

        }
    }
}

Where Room is a basic class I made to help me handle the rooms.

class Room {
    var x1:Int
    var x2:Int
    var y1:Int
    var y2:Int
    var center:CGPoint

    init(X: Int, Y: Int, W: Int, H: Int) {
        x1 = X
        x2 = X + W
        y1 = Y
        y2 = Y + H
        center = CGPoint(x: (x1 + x2) / 2, y: (y1 + y2) / 2)
    }
}

I'm getting a nil value when I shouldn't be. I think I translated the function wrong. Rectangle would be CGRect in Swift but I replaced it with my Room class in other places in the code, so I know it'll work with the Room class here.

How would this function be written in Swift?


Solution

  • Your problem is that you are force unwrapping the result of getRoom - This function returns an optional and can legitimately return nil when your traversal hits a leaf node. Force unwrapping nil results in a crash

    By using conditional unwrapping correctly you can not only make your code more readable, you can eliminate the crash.

    public func getRoom() -> Room? {
    
        if let _ = room {
            return room
        }
    
        let lRoom = leftChild?.getRoom()
        let rRoom = rightChild?.getRoom()
    
        switch (lRoom != nil, rRoom != nil) {
        case (false,false):
            return nil
        case (true,false):
            return lRoom
        case (false,true):
            return rRoom
        case (true,true):
            return arc4random_uniform(2) == 1 ? lRoom: rRoom
        }
    }