If not let - in Swift

XcodeMacosSwift

Xcode Problem Overview


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.

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

Thanks a lot

EDIT: it is a:

var type: JSONDataTypes? = nil

Xcode Solutions


Solution 1 - Xcode

Swift 2.0 (Xcode 7) and later have the new guard statement, 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.

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 else clause of a guard must exit that scope (because otherwise you'd fall into code after that clause, where the guarded variables, like type above, are unbound). So it has to end with something like return, break, continue or 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).

For details, see Early Exit in The Swift Programming Language.


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:

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

Solution 2 - Xcode

Here's how you do it:

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

Solution 3 - Xcode

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:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeter ShawView Question on Stackoverflow
Solution 1 - XcodericksterView Answer on Stackoverflow
Solution 2 - XcodeAbhi BeckertView Answer on Stackoverflow
Solution 3 - XcodeCanView Answer on Stackoverflow