gpt4 book ai didi

ios - Swift 函数指针数组给出编译器段错误 11

转载 作者:行者123 更新时间:2023-11-28 09:04:49 25 4
gpt4 key购买 nike

Swift 在类型检查时无法确定我的类型。我正在使用像这样的函数指针数组:

var directionFuncs = Array< (Tile) -> Tile? >()

init(player1: TileType, player2: TileType)
{
self.player1Type = player1
self.player2Type = player2
tileArray[3, 3] = Tile(column: 3, row: 3, tileType: TileType.Regular, id: 0)
//order matters here because we do opposite directions
directionFuncs.append(tileAbove)
directionFuncs.append(tileBelow)
directionFuncs.append(tileRight)
directionFuncs.append(tileLeft)
directionFuncs.append(tileAboveRight)
directionFuncs.append(tileBelowLeft)
directionFuncs.append(tileBelowRight)
directionFuncs.append(tileAboveLeft)
}

当我尝试像这样使用它时出错:

for var i=0; i<8; i+=2
{
t1 = directionFuncs[i](tile) //compiler complains about this line here
if t1 != nil && !t1!.trapped && t1!.playerID != tile.playerID && !t1!.isMiddle()
{
//opposite direction
t2 = directionFuncs[i+1](tile)
if t2 != nil && !t2!.trapped && t2!.playerID != tile.playerID && !t2!.isMiddle()
{
tilesTrapped.append(tile)
increaseScore(t1!.playerID)
break
}
}
}

奇怪的是,在这段代码之前的循环中,我使用了这样的 for 循环,它工作正常:

for moveDirection in directionFuncs

编译器在做这些事情时会出现段错误:

  1. While type-checking 'checkForTrap' at blah.swift:91:5
  2. While type-checking expression at blah.swift:118:17 - line:118:44] RangeText="t1 = directionFuncsi"

1 引用#2 所在的函数。这里也没有让我感到困惑的返回声明。这是踪迹:

0  swift                    0x00000001099a02b8 llvm::sys::PrintStackTrace(__sFILE*) + 40
1 swift 0x00000001099a0794 SignalHandler(int) + 452
2 libsystem_platform.dylib 0x00007fff834aff1a _sigtramp + 26
3 libsystem_platform.dylib 0x00007fff56977dd0 _sigtramp + 3545005776
4 swift 0x000000010a05f44a swift::TypeBase::isEqual(swift::Type) + 26
5 swift 0x0000000109ec6cb0 std::__1::__function::__func<(anonymous namespace)::favorMatchingOverloadExprs(swift::ApplyExpr*, swift::constraints::ConstraintSystem&)::$_2, std::__1::allocator<(anonymous namespace)::favorMatchingOverloadExprs(swift::ApplyExpr*, swift::constraints::ConstraintSystem&)::$_2>, bool (swift::ValueDecl*)>::operator()(swift::ValueDecl*&&) + 96
6 swift 0x0000000109ec680b (anonymous namespace)::favorCallOverloads(swift::ApplyExpr*, swift::constraints::ConstraintSystem&, std::__1::function<bool (swift::ValueDecl*)>, std::__1::function<void (swift::TypeVariableType*, llvm::ArrayRef<swift::constraints::Constraint*>, llvm::SmallVectorImpl<swift::constraints::Constraint*>&)>) + 411
7 swift 0x0000000109ec5eee (anonymous namespace)::ConstraintOptimizer::walkToExprPre(swift::Expr*) + 1886
8 swift 0x0000000109fe6a50 (anonymous namespace)::Traversal::visit(swift::Expr*) + 5904
9 swift 0x0000000109fe2f75 swift::Expr::walk(swift::ASTWalker&) + 53
10 swift 0x0000000109ec0e29 swift::constraints::ConstraintSystem::optimizeConstraints(swift::Expr*) + 1529
11 swift 0x0000000109ec07e2 swift::constraints::ConstraintSystem::generateConstraints(swift::Expr*) + 194
12 swift 0x0000000109f004e3 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::Type, swift::Type, bool, swift::FreeTypeVariableBinding, swift::ExprTypeCheckListener*) + 499
13 swift 0x0000000109f46969 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 505
14 swift 0x0000000109f46d1a swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 1450
15 swift 0x0000000109f468bf swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 335
16 swift 0x0000000109f46ab1 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 833
17 swift 0x0000000109f468bf swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 335
18 swift 0x0000000109f454b3 swift::TypeChecker::typeCheckFunctionBodyUntil(swift::FuncDecl*, swift::SourceLoc) + 371
19 swift 0x0000000109f45bb6 swift::TypeChecker::typeCheckAbstractFunctionBody(swift::AbstractFunctionDecl*) + 150
20 swift 0x0000000109ef5c4d typeCheckFunctionsAndExternalDecls(swift::TypeChecker&) + 221
21 swift 0x0000000109ef6665 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int) + 1333
22 swift 0x00000001095aad85 swift::CompilerInstance::performSema() + 2661
23 swift 0x0000000109288ef7 frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 2151
24 swift 0x00000001092884e6 main + 1814
25 libdyld.dylib 0x00007fff8eb7f5c9 start + 1

最佳答案

用一个额外的变量解决了这个问题。

//t1 = directionFuncs[i](tile) //this breaks everything.
var dir = directionsFuncs[i]
t1 = dir(tile)

关于ios - Swift 函数指针数组给出编译器段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31102891/

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