Conditional Statements in SwiftUI
Tags: swift, swiftui, ios, mobile
Published: 2020-05-07 18:58:34

---
title: Conditional Statements in SwiftUI
published: true
description: 
tags: swift, swiftui, ios, mobile
canonical_url: https://www.iosapptemplates.com/blog/swiftui/conditional-statements-in-swiftui
---

Although it's undeniable that SwiftUI has made breakthroughs in redefining how to build an iOS app, it still has difficulties building a real application which can get quite complex. Also, while SwiftUI is still a kid - we have to accept some issues as well as use hacky solutions and hope it gets better in the next releases.

In this short article, we will help you solve the seemingly small issues that cause quite a lot of problems in SwiftUI. In this article, I will guide you to solve **the condition statements issue in SwiftUI**

![](https://www.iosapptemplates.com/wp-content/uploads/2020/05/unnamed-300x220.jpg)

## 1. Why is it a problem in SwiftUI?

Now, let's check the following code:

```
var body: some View {
  if hasUserLoggedIn {
    HomeView()
  } else {
    AuthenticationView()
  }
}
```

Do you find this code familiar? If a user had logged in, he would visit HomeView, otherwise, he has to login or sign-up. It makes sense, right? But what happens when we try to build? The compiler will introduce an error as follows _Function declares an opaque return type but has no return statements in its body from which to infer an underlying type_

To understand what's going on, we need to learn a little bit about the concept of opaque return types and prefixed with _**`some`**_ keyword.

We will dive deeper into this at another time. In this article, to avoid getting sidetracked, we are going to make a quick explanation but still making sure it's enough for you to understand the rest.

The main point is that any functions or properties returning an **opaque result type** (represented by **`some`** keyword) must return **the same concrete type**. In the example, depending on the condition, we returned different views (HomeView or AuthenticationView). That's why the compiler complains.

## 2. Some ways to fix it

**Solution 1: Wrap it in a stack view or group**

Since we knew the reason, this solution is quite easy to understand. A stack view will include different views from conditional statements. So, no matter which concrete view was returned, we always have the stack view type. That is enough to make the compiler happy.

```
var body: some View {
  VStack {
    if hasUserLoggedIn {
      HomeView()
    } else {
      AuthenticationView()
    }
  }
}
```

In some previous beta version, we had a view container which helped us implement this easily and probably more cleanly: _ **ConditionalContent<ContentViewIfTrue,ContentViewIfFalse>.**

In terms of mechanism of operations, it is simply just embedding different views into the stack internally, nothing much. For example, you can use **VStack**. However in the official version, it's no longer used anymore. What a pity, hopefully, it will come back soon. 

**Solution 2: Use @ViewBuilder**

In case you do not want to use an extra view such as VStack or Group, you can annotate the body property as **@ViewBuilder**.

```@ViewBuilder
var body: some View {
  if hasUserLoggedIn {
    HomeView()
  } else {
    AuthenticationView()
  }
}
```

But please pay attention to this solution, since developers have reported that some animation behaviors stop working, the toggle, for example. So be careful with this. 

**Solution 3:</span> Wrap in AnyView**

The last solution is using **AnyView**. The name says it all. You can use **AnyView** in order to wrap any different views. With this solution, we have the same view returned.

```
var body: some View {
  if hasUserLoggedIn {
    AnyView(HomeView())
  } else {
    AnyView(AuthenticationView())
  }
}
```

Although no longer have to be embedded in the Stack or need to define **@ViewModifier**, having to see AnyView all the time makes us a little uncomfortable. Every way has its downsides.

## **Conclusion**

In the examples above, we have used if / else to explain the examples but you can also apply these to the switch-case.

Obviously this is not the most expected thing but while waiting for Apple developers to improve this framework, the above solutions are ways to help you deal with condition issues in SwiftUI. Thanks for reading this article and hope this will be helpful to you.

At Instamobile, we're building [React Native Templates](https://www.instamobile.io/) and [iOS App Templates](https://iosapptemplates.com/) to help developers create their mobile apps much faster.

Resources:
* [Product Hunt](https://www.producthunt.com/@tony_hill2)
* [Devbrite](https://devbrite.io/)
* [Expo](https://expo.dev/@floryan2oo7)