0

より読みやすいコードを書くことを期待して、適切な条件を最初にチェックするために、適切な場合はガードステートメントを採用しようとしています。ただし、コンパイル時エラーが発生します。

これが私のコードです:

//guard statements
//guards provide early exits or breaks, placing the error handler code near the conditions.
//Variables are in the same scope as the guard statement
func testGuard(testVar: String?) {
    guard let x = testVar where x.count > 2 else {
        print("failure")
        return
    }
}

ここに私が得ているエラーがあります:

MyPlayground.playground:511:35: エラー: 'count' は利用できません: 普遍的に適切な答えはありません。ディスカッション ガードのドキュメント コメントを参照してください。 let x = testVar where x.count > 2 else { ^~~~~

Swift.String:114:16: 注: 'count' はここでは使用できないと明示的にマークされています public var count: Int { get } ^

4

0 に答える 0