Swift - Array is not convertible to UInt8?

Jake

For some reason I'm getting a compile error with the following code:

func _executeOnBlockAndSurroundingBlocksRecursive(predicate : (block : Block) -> Void,
                      withBlockCondition conditionPredicate : ((block : Block) -> Bool)?,
                                 withStartBlock start_block : Block?,
          withRelativeBlockPositions relativeBlockPositions : [RelativeBlockPosition],
                            withVisitedBlocks visitedBlocks : [Block])
{
    if start_block
    {
        var block : Block = start_block!

        if !visitedBlocks.contains(block)
            && (!conditionPredicate || conditionPredicate!(block: block))
        {
            visitedBlocks += block  //**compile error** - '[Block]' is not convertible to '@lvalue UInt8'

            predicate(block: block)

            for relativeBlockPosition in relativeBlockPositions
            {
                var relativePositionedBlock = block.getBlockForRelativeBlockPosition(relativeBlockPosition: relativeBlockPosition)

                if relativePositionedBlock
                {
                    _executeOnBlockAndSurroundingBlocksRecursive(predicate, withBlockCondition: conditionPredicate, withStartBlock: relativePositionedBlock, withRelativeBlockPositions: relativeBlockPositions, withVisitedBlocks : visitedBlocks)
                }
            }
        }
    }
}

If I change visitedBlocks += block to visitedBlocks.append(block), I get the following compile error:

"Immutable value of type '[Block]' only has mutating members named 'append'

I also tried inout withVisitedBlocks visitedBlocks: [Block] for the method parameter, but that just gave me these compile-time errors:

func _executeOnBlockTypeGroupingOuterEdges(predicate : (edgeBlock : Block) -> Void, withStartBlock block : Block)
{
    _executeOnBlockTypeGroupingOuterEdgesRecursive(predicate, withStartBlock: block, withCurrentBlock: block, withVisitedBlocks: &[]) //**compile error** - '[Block]' is not a subtype of '@lvalue [Block]'
}

func executeOnBlockAndSurroundingBlocksRecursive(predicate : (block : Block) -> Void,
                     withBlockCondition conditionPredicate : ((block : Block) -> Bool)?,
                                      withStartBlock block : Block,
         withRelativeBlockPositions relativeBlockPositions : [RelativeBlockPosition])
{
    _executeOnBlockAndSurroundingBlocksRecursive(predicate, withBlockCondition: nil, withStartBlock: block, withRelativeBlockPositions: relativeBlockPositions, withVisitedBlocks : &[]) //**compile error** - Extra argument 'withBlockCondition' in call
}

Any ideas?

drewag

Arrays in Swift are value types. That means they are always copied when passed into methods. If you want to be able to modify the array and have it affect the array passed in, you have to define the parameter as an inout parameter:

func _executeOnBlockAndSurroundingBlocksRecursive(predicate : (block : Block) -> Void,
                      withBlockCondition conditionPredicate : ((block : Block) -> Bool)?,
                                 withStartBlock start_block : Block?,
          withRelativeBlockPositions relativeBlockPositions : [RelativeBlockPosition],
                      inout withVisitedBlocks visitedBlocks : [Block])

and when you call the method you have to put an & before the array variable.

A better convention for Swift is to return a modified version of the array as a return value:

func _executeOnBlockAndSurroundingBlocksRecursive(predicate : (block : Block) -> Void,
                      withBlockCondition conditionPredicate : ((block : Block) -> Bool)?,
                                 withStartBlock start_block : Block?,
          withRelativeBlockPositions relativeBlockPositions : [RelativeBlockPosition],
                            withVisitedBlocks visitedBlocks : [Block]) -> [Block]

On the recursive call you would be appending to the local copy of the array:

var localCopy = visitedBlocks
localCopy += _executeOnBlockAndSurroundingBlocksRecursive(predicate, withBlockCondition: conditionPredicate, withStartBlock: relativePositionedBlock, withRelativeBlockPositions: relativeBlockPositions, withVisitedBlocks : localCopy)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related