Search code examples
arraysswiftmaxscenekitarkit

Get biggest node from an array


I have an array of SCNNodes and I need to identify which one of them has the highest width.

I know how to calculate the width, but not how to compare among all nodes within the array and return the one with the highest value.

Here’s a sample code:

var walls = [SCNNode]()
   
For wall in walls  {
    let wallWidth = (wall.boundingBox.max.x-wall.boundingBox.min.x)
    print(wallWidth)
}

Thanks in advance!


Solution

  • You can use the max function of array.

    var walls = [SCNNode]()
    let maxNode = walls.max { (left, right) -> Bool in
        let leftWidth = (left.boundingBox.max.x-left.boundingBox.min.x)
        let rightWidth = (right.boundingBox.max.x-right.boundingBox.min.x)
        return leftWidth < rightWidth
    }