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

时间:2015-06-28 17:52:07

标签: ios swift segmentation-fault

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. 在blah.swift:91:5
  2. 进行类型检查'checkForTrap'   
  3. 在blah.swift时使用类型检查表达式:118:17 - line:118:44] RangeText =“t1 = directionFuncsi”
  4.   

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

1 个答案:

答案 0 :(得分:0)

使用额外的变种修复此问题。

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