Result of 'Text' initializer is unused

Hi, in this code it gives me the warning "Result of 'Text' initializer is unused" near Text("(Number)"). Does someone know what exactly mean this warning and how can I fix it?

Thanks in advance


struct NewView: View {
    var body: some View {

        Button {
            tapped()
        } label: {
            Text("hello")
        }
    }
}
             

func tapped() {
    Text("\(Number)")
}

struct NewView_Previews: PreviewProvider {
    static var previews: some View {
        NewView()
    }
}
Answered by Claude31 in 699898022

See the answer in your next post :

struct NewView: View {

    @State var value = 0

    func changeValue() {
        value += 1 
    }

    var body: some View {

        Text("Hello, World! ")
        Button(action: changeValue) {
            Text(value == 0 ? "click me to add a number" : String(value))
        }
    }
}

Your code does not compile, as you have not defined "Number"

The actual error is "Cannot find 'Number' in scope".

Oh ops, now I've defined the Number, but despite it gives me the same warning... my goal is to change the text "hello" to number 1 when I click on it.

var Number = 1

struct NewView: View {
    var body: some View {

        Button {
            tapped()
        } label: {
            Text("hello")
        }
    }
}

             

func tapped() {
    Text("\(Number)")
}


struct NewView_Previews: PreviewProvider {

    static var previews: some View {

        NewView()

    }
}

my goal is to change the text "hello" to number 1 when I click on it

Your code is a long way from achieving that.
It may be best to work through some tutorials first?

Are you studying anything, at the moment?
(There is no point my just writing the solution for you, as I think your current code demonstrates a number of misunderstandings.)

Actually I'm a SwiftUI self-taught, and there's no one who can teach me here, but I'd like to learn it so I try to do what I can but that's not enough... I know that copying others' code makes no sense but also there I try to understand how people arrived at that solution, it isn't very easy sometimes...

So how I can create this button, now?

Accepted Answer

See the answer in your next post :

struct NewView: View {

    @State var value = 0

    func changeValue() {
        value += 1 
    }

    var body: some View {

        Text("Hello, World! ")
        Button(action: changeValue) {
            Text(value == 0 ? "click me to add a number" : String(value))
        }
    }
}
Result of 'Text' initializer is unused
 
 
Q