Trending December 2023 # ‘If Let’ Pyramid Of Doom In Swift: How To Fix It # Suggested January 2024 # Top 20 Popular

You are reading the article ‘If Let’ Pyramid Of Doom In Swift: How To Fix It updated in December 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 ‘If Let’ Pyramid Of Doom In Swift: How To Fix It

In Swift, chained optional binding can lead to an “if let pyramid of doom” that looks like this:

if let a = a { if let b = b { if let c = c { } } }

This is bad practice. However, there is a way to flatten this structure by:

if let a = a, let b = b, let c = c { }

In this guide, you learn how to break the pyramids of doom using neater if-let statements and guard-let statements.

Optional Binding in Swift – A Quick Primer

In Swift, an optional value is something that can be either a value or a nil. Optionals are really common in Swift.

Reading values from optional requires care. This is because if the optional contains nil, the program crashes if you try to do certain actions with it. However, if there is a value, the program works as expected.

To check if an optional consists of a non-nil value, you can use optional binding. This works with the if-let construct.

if let somename = someoptional { } else { }

For example:

var word: String? if let word = word { print(word) } else { print("Nil found") }


Nil found The “if-let Pyramid of Doom”

Once you have multiple optional variables that all need to be non-nil, you end up with a nested “if-let pyramid of doom”.

For example:

var a: String? = "This " var b: String? = "is " var c: String? = "test " if let a = a { if let b = b { if let c = c { print(a + b + c) } } }


This is test

Even though this works, it is bad practice. You should avoid nested code structures as much as possible because they can be hard to read and infeasible to manage.

How to Fix the “if let Pyramid of Doom”

To fix the nested if-let pyramid, you can comma-separate the nested if-let statements and omit using if + curly braces.

For example, this pyramid structure:

var a: String? = "This " var b: String? = "is " var c: String? = "test " if let a = a { if let b = b { if let c = c { print(a + b + c) } } }

Becomes a readable one dimensional if let statement:

var a: String? = "This " var b: String? = "is " var c: String? = "test " if let a = a, let b = b, let c = c { print(a + b + c) }

If the if-let statement becomes too long, you can break it down into multiple lines too:

if let a = a, let b = b, let c = c { print(a + b + c) }

If you are handling optionals in a function, loop, or condition, you can use the guard to get rid of an if-let pyramid.

The guard-let Statement in Functions

The guard-let statement lets you check if an optional has nil or not. If the optional is nil, the guard exits the block of code without going further.

A guard-let statement can be used inside a function, loop, or condition.

The syntax of the guard-let statement is:

guard let somename = someoptional else { return }

A guard-let statement is useful in many ways. One benefit is you can flatten the if-let pyramids using it.

For example, let’s repeat the previous example of optional binding, this time inside a function.

var a: String? = "This " var b: String? = "is " var c: String? = "test " func printOptionals() { if let a = a { if let b = b { if let c = c { print(a + b + c) } } } } printOptionals()


This is a test

Now, let’s use the guard to get rid of the nested if-let pyramid:

var a: String? = "This " var b: String? = "is " var c: String? = "test " func printOptionals() { guard let a = a else { return } guard let b = b else { return } guard let c = c else { return } print(a + b + c) } printOptionals()


This is test

As you can see, the code is now one-dimensional and readable. This piece of code works such that if any of the variables a, b, or c is nil, the function stops and never prints the values.

If you do not prefer the repetition there, make the guard-let statement shorter by:

func printOptionals() { guard let a = a, let b = b, let c = c else { return } print(a + b + c) }


This is test Conclusion

Today you learned how to deal with the “if let pyramid of doom” in Swift.

To recap, the if-let statements are prone to pyramid-like code structures. These are infeasible to manage and difficult to read. To overcome this, you can shorten the if-let statements by comma-separating the optional bindings into one if-let statement.

if let a = a, let b = b, let c = c {

Alternatively, if you are in a code block, use the guard-let statement to get rid of if-let statements altogether.

guard let a = a, let b = b, let c = c else { return }

Thanks for reading.

Happy coding!

Further Reading

50 Swift Interview Questions

You're reading ‘If Let’ Pyramid Of Doom In Swift: How To Fix It

How Guard Statement Works In Swift

Introduction to Swift guard

Swift guard is used to transferring control in the program, it is very simple to use just like if and else in programming. We can use a guard inside any loop, function, etc. Guard statement falls under the control flow statement which is used to transfer the control within the program when passing conditions do not match. If we want to exit the program early or want to redirect the program then we can use the guard statement in Swift.

Start Your Free Software Development Course


As we saw guard statement falls under the category of control flow statement, they are easy and simple to use just like if else.

guard condition else { statements }

This is the official syntax for using the guard statement as per the apple documentation. To define the guard statement in Swift we are just using ‘guard’ keyword. Also, we are passing the condition here. Based on the result we can write our own logic to it whether we want to break, continue or want to throw any exception, etc.


guard let abc = chúng tôi else { break }

As you can see in the above piece of syntax we are just using ‘guard’ keyword to implement the guard statement in Swift.

How guard Statement Works in Swift?

As we know the guard statement is used to transfer the control flow of the program if the condition does not match or satisfy. To implement this statement in our program we just need to use the ‘guard’ keyword followed by the condition. They are pretty much the same as if and else statement and very easy to use and implement.

Here we will first discuss their internal working followed by the sample example.

1. guard

This keyword is used to make the guard statement in Swift, after this statement we can mention our condition. Guard statements are fast and have their own benefits over other control flow statements. By the use of them, we can easily break the statement eerily or we can redirect it to another part of the program. Always remember the ‘guard’ statement is should have someone else blocked associate with it otherwise it will give us an error. Also, the else block should return something from it.

Let’s see points about how the else block of guard statement has some restriction with it that needs to be followed.

2. else block in guard statement

Also, we can pass the control outside the guard statement for which we can use any of the following inside the else block of guard statement:

break: This keyword is used to break the current execution of the program and return back the control to the calling one.

 return: This keyword is used to return any value from the block which can be anything like int, string, nil, etc

 throw: This keyword is used to throw any error, if we want to throw any error inside the else block then we can use this.

 continue: This keyword is used to continue the current execution of the program.

3. return type

while writing the guard statement condition in swift it should return the bool type in Swift. That means the condition should return the bool type otherwise we will receive an error in the program. We can also define the condition of the guard statement as Optional Binding.

Now we will see one sample example of the internal flow of guard statement in swift



guard sample == “hello” else { print(“password is blank”) return “not found” } return “its a match !!” } print(demo(name: “hello”)) print(demo(name: “Hello not “))

Example of Swift guard

Given below is the example mentioned:

In this example we are trying to use a guard statement in Swift, we are trying to match a string passed in the function. based on it we are returning the result as not or not found.


guard name == “hello” else { print(“password is blank”) return “not found” } return “its a match !!” } print(“Demo to show guard statement in Swift !!”) let result1 = demo(name: “hello”) let result2 = demo(name: “bye”) let result3 = demo(name: “world”) let result4 = demo(name: “hello”) let result5 = demo(name: “123”) print(“Printing result :::”) print(“Reuslt one is :::”) print(result1) print(“Reuslt two is :::”) print(result2) print(“Reuslt three is :::”) print(result3) print(“Reuslt four is :::”) print(result4) print(“Reuslt five is :::”) print(result5)



By using the guard statement we can redirect the flow of the program at a very early stage. They are easy to use and implement in Swift just like if and else statement. But they have some benefits over them.

Recommended Articles

We hope that this EDUCBA information on “Swift guard” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

How To Use If Function In Excel

Last Updated on September 5, 2023

The IF function is probably one of the most well-known formulas for Excel, as it allows for data points to be compared between a value and what you are expecting.

IF statements are powerful because you can have two results. The first result is going to inform you of whether your comparison is true. The second result will tell you whether your comparison is false.

Here are the basics of IF functions and how you can use them in Excel.


Example Of IF Function

In column 1 we have a list of Musical Artists, and in column 2 we have Total Album Sales. For reference, we would consider any album sales over 100,000 good, and any album sales over 200,000 excellent.



If Function

If we begin to write “=if(“ into cell C5, you’ll see that it now says logical_test which would represent the Total Album Sales in the second column.



Use Comparison Symbols

What this says is is the value of cell B5 greater than 100,000?



Use A Reference Point

To make this process even easier, you can have cells that already have this value, and then simply use this cell as a reference point. For example, we already have the value 100,000 in cell C2, so we select this instead of writing 100,000.



Use The F4 Key

You can also use the F4 key which will fix the column and row. So the formula will look like this:

If it is greater than 100,000, which in our example it is, then we need to follow up with our formula so that the cell will complete an action.



Use Quotation Marks

We put a comma after the value of 1000,000 and then write “Good”. You must use quotation marks when working out of Microsoft Excel, otherwise use a predetermined cell instead.

Our example now looks like this, with the Good selection being used via cell D2:

To add to this, if you want the cell to do nothing if our Total Album Sales value is less than 100,000 we can simply put in a double quotation mark, whilst closing the bracket at the end of the formula.

This will look like this:

What will happen is that in our third column, if the value of an album sales exceeds 100,000 you will see the word Good appear in those columns, and for any albums that didn’t surpass the 100,000 mark, the cell will remain blank.

You can then use this formula to determine whatever data point you are looking to highlight.



Add Different Labels

In our example, instead of looking for album sales over 100,000, we can look for album sales under 50,000 and make the IF function list them as Bad.

Or we could add different labels such as exceptional if an album went over 200,000 total sales.

Common Issues

There are a couple of common errors that will occur using the IF function. One example is that if you have a “0” value in the cell, this means that there was no argument or selection.

Or perhaps you see “#NAME?” in the cell. This represents that a formula has been misspelled, and as such you must make sure you not only use punctuation marks but also you spell your values correctly.

Final Thoughts

There are many more ways it can be used, but these are the basics. Which should hopefully guide you in the right direction.

Explain Some Common Features Of Protocol Superclass In Swift

In Swift, there are two different concepts i.e. protocols and superclasses are used to build the application by writing reusable and flexible code. They both are different concepts but somehow they have some common features.

You can define the protocol with methods and properties to achieve any particular feature. In the same way, you can create a class with methods and properties to hold some information. Swift provides flexibility in that class, structure, and enums can conform to a protocol.

A superclass is also a class type. It can be inherited from other classes in a similar way. In this case, the superclass can be called a parent class and the inherited class might be called a subclass. This process is called inheritance.

Some common features of protocols and superclasses in Swift include

You can use protocols to define an interface to make a combination of related types. You can define optional methods too in the protocol. While superclasses can be used to define a common blueprint with properties and methods.

The protocols can be conformed by classes, structures, and enums. While superclasses can be used to create subclasses by class type only.

Protocols can be used as a type, which allows you to use polymorphism.

You can define the optional methods in a protocol. Basically, the methods which are not required to implement are always made them optional.

Protocols in Swift are similar to interfaces in other languages, in that they define a set of methods, properties, and other requirements that a conforming type must implement.

In the below example, you will define a protocol named Movable with a method. If any other type conforms to this protocol, it is required to implement this method. We will create a class and conform to the Movable protocol. Let’s see an example to create and conform to the protocol.

Example import Foundation protocol Movable { func move() } class Car: Movable { func move() { } } struct Person: Movable { func move() { } }

In the above example, you have defined a protocol named “Movable”. Now, you are conforming to this protocol in two different types i.e. Car and Person. You can see the protocol can be conformed by class and structure both. You have defined a required method move() in the protocol, which needs to be implemented in Car and Person types.

In the other case, a class is inherited by a superclass. In Swift, the NSObject class is the root class and only the single class from which all classes inherit. For example, a superclass Vehicle is created with some properties and methods such as numberOfWheels and startEngine() that might be common for subclasses. For adopting these properties and methods, you can create subclasses like Car, Bike, Truck, etc. You can add your own properties and methods in the subclasses including the properties of the Vehicle class.

Example class Vehicle { var numberOfWheels: Int func startEngine() { } } class Car: Vehicle { } class Bike: Vehicle { }

You should note down here that protocols and superclasses can be used together. For example, you can define a protocol that a class conforms to, and that class can also inherit from a superclass.

Example protocol Movable { func move() } class Vehicle: Movable { func move() { } } class Car: Vehicle { }

In this example, we created a class called Vehicle that is conforming the Movable protocol. The Movable protocol has a method called move() that is required to be implemented in the Vehicle class.

In the further step, we created a subclass called Car inherited by the Vehicle class. As a result, it can be used both when a Movable is needed and when a Vehicle is required.


Protocols and superclasses are both essential features in Swift programming that play different roles and applications. Protocols can define the methods, properties, and other requirements that a conforming type must implement that allow them to be used interchangeably.

In contrast, superclasses are used to define a base implementation of methods and properties that subclasses can inherit and build on. Protocols and superclasses can also be used in tandem, with a class conforming to a protocol while also inheriting from a superclass.

Understanding how to use protocols and superclasses correctly can help you organize, reuse, and maintain your code.

Opinion: Let The Ipod Touch Rest In Peace – Or Upgrade It Now

Remember the good old days when the iPod touch was the go-to device when you didn’t have an iPhone or iPad? Unfortunately, these days are slowly falling behind us. Apple doesn’t promote any iPods anymore, and it’s time to let this device rest in peace alongside all the other iPods — or upgrade it.

In fact, the iPod touch has been so forgotten by Apple and its users that you really need to search for it on Google to find out the company still sells the 2023 seventh-generation iPod touch in six colors and up to 256GB of storage.

Apple touted that the current iPod touch is all about the “fun at full speed.” The company even promoted the iPod touch as a gaming device before releasing its Apple Arcade service. Now, with over 180 games on its gaming subscription platform and three years later, the iPod touch feels lost in time.

It runs the A10 Fusion chip, found in the iPhone 7. Although it’s twice as fast as its previous model and has three times better graphics performance, with only a 4-inch Retina display, the iPod touch feels too small. It has an 8MP camera capable of video recording in 1080p HD and has a FaceTime HD camera with 1.2MP resolution.

With an outdated camera and display size, and almost an outdated chip, will Apple try to revive the iPod glory in an eighth-generation?

What’s the point of having an iPod touch in 2023

With Family Sharing set up, the kid can also benefit from iCloud, asking for an adult to approve in-app payments, and it can always be found with the Find My feature. For an adult or an older person, the iPod touch will feel too small to read or play games. The iPod touch speakers also leave a lot to be desired.

If we compare the iPod touch with a gaming console, let’s go back to 2023. At the time of the seventh-generation iPod touch launched, Nintendo was still a few months away from introducing the $199 Nintendo Switch Lite practically created for kids. It has a more resistant design, unattachable controllers, and access to classic Nintendo games. Its larger screen compared to the iPod touch is also important to note.

When the Japanese company introduced a cheaper version of its successful Nintendo Switch console, the iPod touch also lost its “gaming device” benefit. In 2023, it’s way more noticeable. Here’s what my colleague Jeff Benjamin had to say about the iPod in 2023:

“How does the iPod touch 7th-gen work as a portable video game machine? I’ve found that it works fairly well if you consider a few potential caveats. The most obvious downside is screen size. While many of the games work well on the 4-inch display of the iPod touch, some games, like the third-person runner Hot Lava, are a stretch to play on such a small display.”

“For adults, this may be especially true, since bigger hands can easily obscure the display, which also serves as the primary controller for games on the platform. Using an external Bluetooth controller helps — iOS 13 adds PlayStation Dual Shock 4 and Microsoft Xbox One controller support — but such a setup isn’t exactly ideal for a device with a 4-inch display.“

What Apple could do about the iPod touch?

Apple has two-way outs with the iPod touch: it can upgrade it with better cameras, a larger display, and a faster processor with the A12 Bionic or superior. Or the company can say goodbye to the iPod once and for all.

As WWDC 2023 approaches, it could be a great time for Apple to announce one of the two things: the iPod line going away for good or giving it a well-deserved upgrade. iOS 15, for example, will probably stop supporting the iPhone 6s. With that in mind, the iPod touch and the A10 Fusion processor could be supported for just one more year — giving new buyers of the iPod only one major software update.


FTC: We use income earning auto affiliate links. More.

Ipad Not Showing Up In Sidecar? How To Fix It

Is your iPad not showing up in Sidecar? As per reports, the problem occurs during both wireless and wired connections. 

Though I haven’t faced any such issues on my Mac, I can spot the culprits hidden behind this issue—primarily based on previous experience. So, if you’re on the lookout to fix a broken Sidecar, you’re at the right troubleshooting guide. 

Let’s get rid of that snag right away!

8 Ways to fix iPad not showing up in Sidecar issue

1. Ensure that your devices are compatible

12.9-inch iPad Pro

11-inch iPad Pro

10.5-inch iPad Pro

9.7-inch iPad Pro

iPad Air (3rd-gen)

iPad (6th-gen or later)

5th-gen iPad mini

Macs that support Sidecar

MacBook Pro 2023 or later

MacBook Air 2023 or later

MacBook 2023 or later

Mac Pro 2023 or later

iMac Pro 2023 or later

iMac 2023 or later

Mac mini 2023 or later

Note: While there is a complex workaround to use Sidecar even on an unsupported Mac, I don’t recommend it to a normal user as it doesn’t work most of the time. So, there’s no point in bringing it into the discussion.

2. Keep two-factor authentication enabled

Apple has made two-factor authentication a must for most new features like Messages in iCloud and Approved. Sidecar is no exception to this rule. So, if you’ve turned off 2FA, make sure to enable it so you can use Sidecar.

On Your iPad: Open Settings → Profile photo (avatar) → Password & Security → Two-Factor Authentication. Now, ensure it’s enabled.

On Your Mac: Launch System Preferences → Apple ID → Password & Security → Two-Factor Authentication. Now, make sure the 2FA option is ON.

3. Turn off/on Wi-Fi, Bluetooth, and Handoff

For Sidecar to work, both of your devices have to be on the same Wi-Fi connection. It also requires Bluetooth and Handoff be enabled on your iPad and Mac. 

If you’re facing the problem even after checking off these essential boxes, try turning off/on Wi-Fi, Bluetooth, and Handoff. Restarting your devices could prove useful as well.

Disable Wi-Fi and Bluetooth

On your iPad, head into Settings → Wi-Fi/Bluetooth. Now, turn them off.

Disable Handoff

On your iPad: Head over to Settings → General → Handoff, then turn on Handoff.

Now, restart both of your devices. After they reboot, turn on Wi-Fi, Bluetooth, and Handoff on your Mac and iPad. 

Note: To use Sidecar wirelessly, keep your devices within 10 meters (30 feet) of each other.

4. Ensure that your iPad and Mac aren’t sharing internet

Sidecar won’t work if your iPad and Mac are set to share an internet connection. Maybe you had enabled Internet sharing on your device and forgot to turn it off. Check it out as it might well be the reason behind this issue.

On Your iPad, go to Settings → Personal Hotspot and then toggle it off.

On Your Mac, open System Preferences → Sharing. Now, uncheck the box for Internet Sharing.

5. Ensure that your iPad is set to trust your Mac

Especially when using Sidecar over USB, ensure that your iPad is set to trust your Mac. When you connect your iPad to the macOS device using a cable, a popup will appear on the tablet asking you to trust the computer. Tap Trust.

Just in case the popup to Trust the computer doesn’t appear, reset location and privacy. Launch Settings on your iPad → General → Reset. Now, tap on Reset Location & Privacy and confirm.

Connect your tablet to the computer. The Trust popup will now appear.

6. Disable Block All Incoming Connections

Have you enabled Firewall on your Mac? Buried inside the Firewall setting is an option to block all the incoming connections except those needed for basic internet services like IPSec, DHCP, Bonjour, etc. 

As it’s known to create connection problems, you might benefit from turning it off.

One thing worth mentioning is that you don’t need to turn off the Firewall to disable this feature.

7. Sign out of Apple ID and sign back in

Another requirement for Sidecar is that your devices must be signed in to iCloud with the same Apple ID. So, be sure you have ticked off this requirement as well. 

If you’re facing the problem even after meeting this requirement, sign out of Apple ID on your iPad and Mac. Then, sign back in.

On iPad: Open Settings → Profile photo → Sign Out.

On Mac: Open System Preferences → Apple ID → Sign Out.

Now, sign in using the same Apple ID on both the devices.

If none of the tricks have fixed your iPad not showing in Sidecar, a software bug could be behind it. If your devices aren’t up to date, now’s the time. Most likely, you’ll be able to finally resolve the problem with a software update.

On iPad: Open Settings → General → Software Update.

Hopefully, you got the better of the problem and also found out how to deal with the common snags that tend to pop up at times.

You may also like to read:

Author Profile


Marcus is a freelance tech writer/editor with a focus on succinctly explaining consumer devices and their software. His previous work has been published on MakeUseOf where he covered everything from iOS to Git and UI design.

Update the detailed information about ‘If Let’ Pyramid Of Doom In Swift: How To Fix It on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!