cannot find text1 in scope

import SwiftUI

struct viewdetail: View {
    @State var text1:String = ""
    @State var tip1:String = ""
    @State var text23:String = ""
    @State var tip23:String = ""
    
    var body: some View {
        Text(text1);Text(tip1);Text(text23);Text(tip23)
    }
}
func detailLine(costa:inout [Double],tipa:inout [Double]) {
    print(costa,tipa)
    text1 = "125"                                                  cannot find 'text1' in scope
    print("detail")
}

func delete(costa:inout [Double],tipa:inout [Double],number: inout Int) {
        print(costa,tipa)
        tipa.removeLast()
        costa.removeLast()
        number -= 1
        print(costa,tipa)
    }

#Preview { viewdetail() }

Does anyone know why I'm getting "cannot find text1 in scope"?

HI, it is about Scope. this will help: youtube short video: [https://youtu.be/kYl27LjJ594)

If I remember, you have already asked the same question several time. And it seems you have not read the answers.

You should learn more about Swift to understand the basic principles.

In Swift, an object has a visibility scope: it is visible inside the block where it is defined. In your case, detailLine is declared outside viewdetail (should be written ViewDetail according to Swift naming conventions). As text1 is declared inside ViewDetail, it is not visible in detailLine.

Once again, please post complete code so that we understand what you want to do. It seems the architecture of your code has several issues.

Thank you.

If I put func detailLine in the curly braces I get another error. I get:

Closure containing a declaration cannot be used with result builder 'ViewBuilder'

Also I get cannot find detailLine in scope. Please help.

I was asked to send the code where I call detailLine. I did that but I never got the answer. I'm still waiting. Please answer.

I was asked to send the code where I call detailLine. I did that but I never got the answer. I'm still waiting. Please answer.

If I remember well, I did answer (https://developer.apple.com/forums/thread/771287) that the code was not enough. Where do you call enterp() ? We need to understand how the whole code works.

As you have multiple posts about essentially the same question, that makes things harder to track.

I call enterp from a button. The button is in var some body. I'll send the code. Look at the bottom.


  var body: some View {
        NavigationStack {
            VStack
            {
                Text("                                                                  " + Date().formatted(date: .numeric, time: .omitted))
                    .foregroundColor(.black)
                    .fontWeight(.bold)
                Text("Driver's Food Delivery")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                Image("Screenshot 2024")
                HStack
                {
                    Text("Cost")
                        .font(.largeTitle)
                        .padding(10)
                    Spacer()
                    Text("Paid")
                        .font(.largeTitle)
                        .padding(10)
                    Spacer()
                    Text("Tip")
                        .font(.title)
                    Spacer()
                    Text("Tip %")
                        .font(.title)
                }
                HStack
                {
                    TextField("Cost", text: $cost)
                        .frame(width: 75, height: 25)
                        .padding(20)
                        .fontWeight(.bold)
                        .font(.system(size: 22))
                        .foregroundColor(.blue)
                        .background(Color.brown)
                        .cornerRadius(025)
                    TextField("Paid", text: $paid)
                        .frame(width: 75, height: 25)
                        .padding(20)
                        .fontWeight(.bold)
                        .font(.system(size: 22))
                        .foregroundColor(.blue)
                        .background(Color.brown)
                        .cornerRadius(025)
                    Text(tipp)
                        .frame(width: 95, height: 25)
                        .background(Color.white)
                        .fontWeight(.bold)
                        .font(.system(size: 22))
                        .foregroundColor(.blue)
                    Text(tipc)
                        .frame(width: 75, height: 5)
                        .padding(10)
                        .background(Color.white)
                        .fontWeight(.bold)
                        .font(.system(size: 22))
                        .foregroundColor(.blue)
                }
                VStack
                {
                    Text (emsg)
                        .foregroundColor(.red)
                    Text (msg)
                        .foregroundColor(.blue)
                    HStack {
                        Text (msg2)
                            .foregroundColor(.blue)
                        Spacer()
                    }
                    Button { enterp()
                    }
                    label: { Text("Enter")
                            .font(.largeTitle)
                            .foregroundColor(.green )
                    }
  
cannot find text1 in scope
 
 
Q