2016-04-12 6 views
0

なぜcontinueは、エラーフラグを立てるれる:スイフト言語:ガード・ステートメントの後に続行するには?

continue is only allowed inside a loop

private func addToUnloadedImagesRow(row: Int, forLocation:String!) { 
    guard unloadedImagesRows[forLocation] != nil else { 
     unloadedImagesRows[forLocation] = [Int]() 
     continue 
    } 
    unloadedImagesRows[forLocation]!.append(row) 
} 

どのように私はこの問題を解決することができますか?

+1

関数を再開する場合は、代わりにifステートメントを使用する必要があります。 – zneak

答えて

1

continueloopsのステートメントを使用してください。

複数の条件を確認する場合は、guardの代わりにifステートメントを使用する必要があります。

2

ガードステートメントの後に現在のスコープ(あなたのケースではaddToUnloadedImagesRow(_:forLocation:)メソッド)を「続行」できません。ガードステートメントのelseブロックは、が現在のスコープから離れる必要があります。

はとにかく、あなたのコードを見て、私はあなただけでこれをしたいと思う:この中

private func addToUnloadedImagesRow(row: Int, forLocation:String!) { 
    guard unloadedImagesRows[forLocation] != nil else { 
     unloadedImagesRows[forLocation] = [row] 
     return 
    } 
    unloadedImagesRows[forLocation]!.append(row) 
} 

しかし、個人的に:

private func addToUnloadedImagesRow(row: Int, forLocation:String!) { 
    if unloadedImagesRows[forLocation] == nil { 
     unloadedImagesRows[forLocation] = [Int]() 
    } 
    unloadedImagesRows[forLocation]!.append(row) 
} 
2

あなたが本当にguardを使用したい場合は、これを行うことができます私はifを読むのがやや簡単だと思います。

関連する問題