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: error: 'count' is available: 没有普遍好的答案,请参阅讨论守卫的文档评论 let x = testVar where x.count > 2 else { ^~~~~

Swift.String:114:16:注意:'count' 已在此处明确标记为不可用 public var count: Int { get } ^

4

0 回答 0