How to Shorten if let variable = optionalVariable { }

Hi, beginner here. So I have read how 1 way to use an optional without having to be forced to unwrap it with ! is this ...

if let var1 = optionalVar1 {

}

This is not a prob. But what if i have too many optionals? I do not want to have nested if let blocks. Is there a shorter way not to do too many ifs that without having to unwrap it with ! ?

e.g.

if let var1 = optionalVar1 {
   if let var2 = optionalVar2 {
        if let var3 = optionalVar3 {

       }
   }
}
Answered by robnotyou in 732372022

You can separate your cases with commas, like this:

if let var1 = optionalVar1,
	let var2 = optionalVar2,
	let var3 = optionalVar3 {
}

...or use the new (Xcode 14) shorthand, like this:

if let optionalVar1,
	let optionalVar2,
	let optionalVar3 {
}
Accepted Answer

You can separate your cases with commas, like this:

if let var1 = optionalVar1,
	let var2 = optionalVar2,
	let var3 = optionalVar3 {
}

...or use the new (Xcode 14) shorthand, like this:

if let optionalVar1,
	let optionalVar2,
	let optionalVar3 {
}
How to Shorten if let variable = optionalVariable { }
 
 
Q