xcode 如果不让 - 在 Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27412735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:20:55  来源:igfitidea点击:

If not let - in Swift

xcodemacosswift

提问by Peter Shaw

is there is a way to negate the "if let" in swift? This looks silly to me:

有没有办法快速否定“如果让”?这对我来说看起来很傻:

    if let type = json.type  {

    } else {
        XCTFail("There is no type in the root element")
    }

I can't use XCTAssertNotNil, because json.type is a enum.

我不能使用 XCTAssertNotNil,因为 json.type 是一个枚举。

enum JSONDataTypes {
    case Object
    case Array
    case Number
    case String
}

Thanks a lot

非常感谢

EDIT: it is a:

编辑:它是一个:

var type: JSONDataTypes? = nil

采纳答案by rickster

Swift 2.0 (Xcode 7) and later have the new guardstatement, which sort of works like an "if not let" -- you can conditionally bind a variable in the remainder of the enclosing scope, keeping the "good path" in your code the least-indented.

Swift 2.0 (Xcode 7) 及更高版本有新guard语句,它的工作方式类似于“if not let”——您可以有条件地在封闭作用域的其余部分绑定一个变量,在代码中保持“良好的路径”最少缩进。

guard let type = json.type else {
    XCTFail("There is no type in the root element")
}
// do something with `type` here

The catch to this is that the elseclause of a guardmust exit that scope (because otherwise you'd fall into code after that clause, where the guarded variables, like typeabove, are unbound). So it has to end with something like return, break, continueor a function that is known to the compiler to never return (i.e. annotated @noreturn, like abort()... I don't recall offhand if that includes XCTFail, but it should (file a bug if it's not).

对此的捕获是elsea的子句guard必须退出该范围(因为否则您将陷入该子句之后的代码中,其中受保护的变量,type如上所示,未绑定)。因此,它具有的东西,如结束returnbreakcontinue或已知的编译器永不返回(即带注释的功能@noreturn,就像abort()......我不记得不加思索如果包括XCTFail,但应(文件,如果它不是一个bug)。

For details, see Early Exitin The Swift Programming Language.

有关详细信息,请参阅The Swift Programming LanguageEarly Exit



As for really-old stuff... There's no negated form of if-let in Swift 1.x. But since you're working with XCTest anyway, you can just make testing the optional part of an assertion expression:

至于真正古老的东西...... Swift 1.x 中没有 if-let 的否定形式。但是由于您无论如何都在使用 XCTest,您可以只测试断言表达式的可选部分:

XCTAssert(json.type != nil, "There is no type in the root element")

回答by Abhi Beckert

Here's how you do it:

这是你如何做到的:

if json.type == nil {
  // fail
}

回答by Can

Another alternative I've used a few times:

我使用过几次的另一种选择:

switch json.type
{
    case .None: // ...
    case .Some(.Object): // ...
    case .Some(.Array):  // ...
    case .Some(.Number): // ...
    case .Some(.String): // ...
}

Since the ?is actually Optional<T>which is an enum on its own, defined as:

由于?is 实际上Optional<T>本身就是一个枚举,因此定义为:

enum Optional<T> : Reflectable, NilLiteralConvertible 
{
    case None
    case Some(T)

    ...
}