Learn Courses My Dashboard

Result of 'Text' initializer is unused

Result of ‘Text’ initializer is unused!

content view .swift code attached`//
// ContentView.swift
// Menue App
//
// Created by Ansh on 12/05/21.
//

import SwiftUI

var Combo1 = "Dosa Sambar Chutney "
var Combo2 = “Varan Bhat”
var Combo3 = " Bengan Bharta "
var Combo4 = “Patta Gobi Sabji And Masoor Amti”

struct ContentView: View {
var body: some View{

    VStack{
        
        
        
        var  RESULT:String = "xyz"

        

        
        Spacer()
        Button("Generate Random Menu" ) {
            let randomNumber = Int(arc4random_uniform(4) + 1)
            
            if randomNumber==1{RESULT="Dosa"}
            if randomNumber==2{RESULT="Varan Bhat"}
            if randomNumber==3{RESULT="Bengan Bharta"}
            if randomNumber==4{RESULT="Patta Gobi Sabji And Masoor Amti"}
            var finalResult = ("The Menu For Today is"+RESULT )
            
            Text(finalResult)
            }
        Spacer()
        
        
        
        
        
        
        }
    }
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
`

Hi @EASYCOMIN

I hope the following helps you to understand a little better how SwiftUI works.

import SwiftUI

struct ContentView: View {
    @State private var selectedMenuItem = ""
    var menuItems = ["Dosa Sambar Chutney",
                "Varan Bhat",
                "Bengan Bharta",
                "Patta Gobi Sabji And Masoor Amti"]

    var body: some View{
        VStack {
            Button("Generate Random Menu" ) {
                let randomNumber = Int(arc4random_uniform(4))
                selectedMenuItem = menuItems[randomNumber]
            }
            .padding(.bottom)

            Text("The menu for today is:")
            Text(selectedMenuItem)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Here the menu items are in an array rather than being separately declared as you had done. The random number generated is used as an index to select an element from the array which is assigned to selectedMenuItem.

When you want to change the value of a variable in a SwiftUI struct you have to declare it as a @State variable. This makes it mutable. Normally properties in a struct are immutable which means that you can’t change them but @State is a special property wrapper that allows you to change the contents of a variable.

In the body of the View, the elements are always placed in the center by default so that means you don’t need to place a Space() above and below in order to ensure that they are in the center.

Anyway I hope you can make sense of what I have done.