What is the syntax for "if not" in Swift?

I have the code below which works just fine. getTwinklingGem returns type MyGem.

What I cannot figure out is if there is a proper syntax for writing the NOT into the if statement. Am still too new to Swift.

This works, but seems lengthy:

if let twinkling = getHighGem(), twinkling != nil

Is this the proper way to test for a nil return?

if let twinkling = getHighGem() as? MyGem

if let twinkling = getTwinklingGem() {
 print ("not nil")
}

if-let is a syntax including non-nil test, so writing twinkling != nil after if let twinkling = ... is not needed.

Is this the proper way to test for a nil return?

YES.

Sorry, I wanted the NOT (!) equivalent to : if let twinkling = getHighGem() as? MyGem

There are a few things you can do. You can do if let twinkling = getHighGem() else {...}, like you mentioned.

You could also do guard let twinkling = getHighGem() else {... return}.

Additionally, you could change getHighGem() -> MyGem? to getHighGem() throws -> MyGem so that instead of returning nil, you throw an exception. In this case, you would need to catch the exception when you call it. I put some example code below

import Foundation

func nilFunc(x: Int) -> Int? {
    if (x == 1) {
        return 1
    }
    return nil
}

func throwFunc(x: Int) throws -> Int {
    if (x == 1) {
        return 1
    }
    throw NSError()
}

func test1(x: Int) {
    guard let result1 = nilFunc(x: x) else {
        print("result1 is null")
        return
    }
    print("result1 is \(result1)")
}

func test2(x: Int) {
    var result2: Int
    do {
        result2 = try throwFunc(x: x)
    } catch {
        print("result2 is null")
        return
    }
    print("result2 is \(result2)")
}

test1(x: 1) //1
test1(x: 0) //null
test2(x: 1) //1
test2(x: 0) //null

But this would then go when all I really want is the not part if let twinkling = getHighGem() as? MyGem { } else { ..... }

Sorry, I wanted the NOT (!) equivalent to : if let twinkling = getHighGem() as? MyGem

I cannot be sure if I would be interpreting what you mean correctly, but you want to find some sort of better alternative for this?

if let twinkling = getHighGem() as? MyGem { /* Do nothing */ } else {
    .....
}

As this type of code:

if someCondition { /* Do nothing */ } else {
    .....
}

can be re-written using not ! operator:

if !someCondition {
    .....
}

If you want to do something like above, you need to think what if-else would do on true-case and false-case:

The if-let first checks the Optional value of the right hand side if it is nil or not:

  • And if it is not nil, it does a little more
    1. Unwrap the Optional value and retrieve the non-Optional value
    2. Create a new binding to the unwrapped value as a local constant
    3. Execute the true-case code block
  • And if the checked value is nil
    1. Execute the false-case code block

So, if you just want a false-case execution, you just need a nil checking:

if (getHighGem() as? MyGem) == nil {
    .....
}

In this case, it can be rewritten as:

if !(getHighGem() is MyGem) {
    .....
}

But, as you can see in the answer of Graphics and Games Engineer, using guard-let-else would be a better alternative in various cases. If you had shown enough context, you would have been shown which is better.

What is the syntax for "if not" in Swift?
 
 
Q