gpt4 book ai didi

arrays - Swift - 数组不能转换为 UInt8?

转载 作者:行者123 更新时间:2023-11-28 09:20:45 24 4
gpt4 key购买 nike

出于某种原因,我在使用以下代码时遇到编译错误:

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)
}
}
}
}
}

如果我将 visitedBlocks += block 更改为 visitedBlocks.append(block),我会收到以下编译错误:

“类型‘[Block]’的不可变值只有名为‘append’的可变成员

我还尝试了 inout withVisitedBlocks visitedBlocks: [Block] 作为方法参数,但这只是给了我这些编译时错误:

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
}

有什么想法吗?

最佳答案

Swift 中的数组是值类型。这意味着它们在传递给方法时总是被复制。如果你希望能够修改数组并让它影响传入的数组,你必须将参数定义为 inout 参数:

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

并且当您调用该方法时,您必须在数组变量之前放置一个 &

Swift 的一个更好的约定是返回数组的修改版本作为返回值:

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

在递归调用中,您将追加到数组的本地副本:

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

关于arrays - Swift - 数组不能转换为 UInt8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24772085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com