'An if statement that shows text "Your list is empty" if the List view is empty

I am a beginner iOS developer using swiftUI and I want to create an if statement that shows the text "Your list is empty" if the list in the TaskView is empty. What should I put inside of the if else statement? Here is my code:

import SwiftUI

struct TasksView: View {
    @EnvironmentObject var RealmManager: RealmManager
    
    var body: some View {
        VStack {
            Text("My List")
                .font(.title3).bold()
                .foregroundColor(.black)
                .padding()
                .frame(maxWidth: .infinity, alignment: .leading)
            
            List {
                ForEach(RealmManager.tasks, id: \.id) {
                    task in
                    if !task.isInvalidated {
                        TaskRow(task: task.title, completed: task.completed)
                        .onTapGesture {
                            RealmManager.updateTask(id: task.id, completed: !task.completed)
                        }
                        .swipeActions(edge: .trailing) {
                            Button(role: .destructive) {
                                RealmManager.deleteTask(id: task.id)

enter image description here



Solution 1:[1]

You can simply use an if statement inside your view, like this:

            Text("My List")
                .font(.title3).bold()
                .foregroundColor(.black)
                .padding()
                .frame(maxWidth: .infinity, alignment: .leading)
          
          // Here is the condition:
          if RealmManager.tasks.isEmpty {
            Text("Your list is empty")
          } else {
            
            List {
                ForEach(RealmManager.tasks, id: \.id) {
                    task in
                    if !task.isInvalidated {
                        TaskRow(task: task.title, completed: task.completed)
                        .onTapGesture {
                            RealmManager.updateTask(id: task.id, completed: !task.completed)
                        }

Solution 2:[2]

Simple solution with the ternary conditional operator

Text("My List\(realmManager.tasks.isEmpty ? " is empty" : "")")

Please name variables with starting lowercase letter, var RealmManager: RealmManager is pretty confusing

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HunterLion
Solution 2 vadian