Guard语句参数错误

时间:2016-07-29 16:59:01

标签: swift guard-statement

为了编写更易读的代码,我试图在适用的情况下使用保护语句来首先检查正确的条件。但是,我遇到编译时错误。

这是我的代码:

//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:错误:'计数'不可用:那里   不是普遍的好答案,请参阅文档评论   讨论       guard let x = testVar其中x.count> 2其他{                                     ^ ~~~~

     

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

0 个答案:

没有答案