53

I'm trying to write the following block of code in a single if let line:

 if let amount = datasource?.incrementForCount?(count) {

        count += amount
    }

    else if let amount = datasource?.fixedIncrement {
        count += amount
    }

when I try something like:

 if let amount = datasource?.incrementForCount?(count) ||  let amount = datasource?.fixedIncrement {

        count += amount
    }

I got a compile time error.

I don't think that where clause is possible for this case.

Is it possible to combine the two if let statements into a single ORed one?

0

2 Answers 2

61

Try to use ?? operator:

if let amount = datasource?.incrementForCount?(count) ?? datasource?.fixedIncrement 
{
    count += amount
}

is it possible to combine the 2 if let statements into a single ORed one ?

It is possible to have several let statements like

if let a = optionalA, b = optionalB { ... }

And all of them should return non-nil value to pass.

But if you also want to use a logical condition it:

  1. Could be only the one
  2. Should be placed on the first place, before any let statements
Sign up to request clarification or add additional context in comments.

3 Comments

is there a cleaner solution to use when there are further ORed conditions instead of nesting ?? operator statetments ?
@JAHelia No. You can have only one logical condition in if statement (not where) and it should be placed on the first place, before any let statements.
I don't know when it was changed but Swift 5.4 requires the following syntax: if let a = optionalA, let b = optionalB { ... }
19

No, you can't OR two if-let statements, or even an if-let statement with a non if-let condition, because that would completely defeat the purpose of the if-let statement. if-let ensures that you can only enter the body of the statement if the optional could be successfully unwrapped into the variable.

let str:String? = nil
if let s = str || true {
  //s is nil, yet you're still in the if-let body
}

In the above example you would still enter the optional body even if s were nil, in which case you would still have to check whether sis nil or not inside the body, rendering the first if-let check pointless.

2 Comments

Disagreed. There are instances where you need to use an optional when a condition is met, but if that condition's not met you don't need to use that optional. Example: you need an ID to initialize a controller, but depending on what the server gives, you might use the default value. This is when it's best to combine if let and || since the init code is the same. This is convenient: if let id = model.id || model.usesDefaultValue { /*init code here once*/ } This is not: if let id = model.id { /*init here*/ } else if model.usesDefaultValue { /*same init - code repetition*/ }
This is correct -- to clarify, let's say Swift tried to allow that statement. When if let s = str evaluates to true, that means that s is not an optional, but when evaluates to false, that means s would need to be an optional. s cannot both be an optional and a non-optional at the same time, so it can't compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.