cannot find in scope

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")
}
Answered by Claude31 in 819035022

It's normal, it is out of scope :

If you format the code properly:

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 îs defined OUT of viewDetail.

Hence, text1 in detailing is not defined.

You have to change as follows:

struct ViewDetail: View {
    @State var text1:String = ""
    @State var tip1:String = ""
    @State var text23:String = ""
    @State var tip23:String = ""
    
    func detailLine(costa:inout [Double],tipa:inout [Double]) {
        print(costa,tipa)
        text1 = "125"              //    Now, Can find 'text1' in scope
        print("detail")
    }

    var body: some View {
        Text(text1);Text(tip1);Text(text23);Text(tip23)
    }
}

Note also I changed the caps on names to follow Swift rules.

PS: when you ask a question:

  • format code with code formatter tool
  • take time to formulate the question in the text, not only the title.
  • Don't forget to close the thread once you've got the correct answer, by marking the answer as correct.
Accepted Answer

It's normal, it is out of scope :

If you format the code properly:

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 îs defined OUT of viewDetail.

Hence, text1 in detailing is not defined.

You have to change as follows:

struct ViewDetail: View {
    @State var text1:String = ""
    @State var tip1:String = ""
    @State var text23:String = ""
    @State var tip23:String = ""
    
    func detailLine(costa:inout [Double],tipa:inout [Double]) {
        print(costa,tipa)
        text1 = "125"              //    Now, Can find 'text1' in scope
        print("detail")
    }

    var body: some View {
        Text(text1);Text(tip1);Text(text23);Text(tip23)
    }
}

Note also I changed the caps on names to follow Swift rules.

PS: when you ask a question:

  • format code with code formatter tool
  • take time to formulate the question in the text, not only the title.
  • Don't forget to close the thread once you've got the correct answer, by marking the answer as correct.

Now I get cannot find detailLine in scope. Where is the code formatter tool?

detailLine(costa: &costa,tipa: &tipa) } Cannot find 'detailLine' in scope

The code you post doesn't show where you call detailLine. How do you want anyone to guess ?

Please show comprehensive code so that we can understand what you are trying to do.

Code formatter is the 4th icon from the left below the Edit area when you edit a post.

  • select the whole code text
  • click on the formatter tool
    func enterp()
{
    var ipc = 50.0
    var ipcc = 50.0
    var tippp = 0.0
    var totalcsh = 0.0
    var ttips = 0.0
    var tcost = 0.0
    var tipo = 0.00
    costt = Double(cost)
    paidt = Double(paid)
    emsg = ""
    if (costt != nil) && (paidt != nil) {
        tipo = (Double(paid)! - Double(cost)!)
        tipa.append (tipo)
        costa.append (Double(cost)!)
        number += 1
        numbc = String(number)
        ttips = (tipa.reduce(0, +))
        tcost = (costa.reduce(0,+))
        tippp = (ttips / Double(number))
        tipaa = tippp.formatted(.currency(code: "USD"))
        totaltips = ttips.formatted(.currency(code: "USD"))
        totalcost = tcost.formatted(.currency(code: "USD"))
        tipp = tipo.formatted(.currency(code: "USD"))
        tipc = String(tipo / Double(cost)! * 100)
        ipc = Double (tipc)!
        tipc = String(format: "%3.0f%%", ipc)
        tipcc = String(ttips / tcost * 100)
        ipcc = Double (tipcc)!
        tipcc = String(format: "%3.0f%%", ipcc)
        totalcsh = (tcost + ttips)
        totalcash = totalcsh.formatted(.currency(code: "USD"))
    } else {
            emsg = "Enter numbers and 1 decimal point only."
            }
    detailLine(costa: &costa,tipa: &tipa) }
    }

Thanks for the code, but still difficult to understand how all parts fit together.

Where do you call enterp() ?

  • Is it from inside ViewDetail (in a Button action for instance) ?

If so, enterp() should also be inside ViewDetail

  • Elsewhere, from another View ?

If so, then you should pass text1 as a Binding when calling the other View

Problem is that text1 is both a State var and you want to update in a conventional func which is not View related.

Please let us understand the overall architecture of your code.

Yes I call enterp() from a button. But it's not in viewdetail. It's in the main program(var some body).

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 )
                    }
I'm going to have text1 to text44.  What else do you need to know?

Did you find out what's the problem?

Please help.

It is very simple so.

Let's assume the view that contains enterp() is called AnotherView.

Then, in AnotherView

  • define a Binding
@Binding var text1ForUpdate: String  // To make it easier to understand, I've not named it text1, but you could
  • declare detailLine inside AnotherView
  • somewhere in ViewDetail you call AnotherView
  • pass the text1 as parameter:
AnotherView(text1ForUpdate: $text1)

Change ne name in detailLine func:

text1ForUpdate = "125"

Tell if that works now.

Can I do this if I'm gonna have 44 Texts(text1 thru text44)? I'll have 44 arguments to pass in. Enterp is in some view. How do I call it?

Let me explain. The user(driver) enters the cost of the delivery and the amount he got paid. The program(enterp) figures out the tip, tip %, average tip, total costs, total tips, total tip % and total cash and writes it out to the screen. Using detailLine I want to print all deliveries(up to 44) and tips to the second screen. I hope this helps.

Can I do this if I'm gonna have 44 Texts(text1 thru text44)? I'll have 44 arguments to pass in. Enterp is in some view. How do I call it?

Put the 44 text in an array. It will be the State var and that is what you'll pass for Binding.

Thanks for answering. I'm sorry I'm not sure how to go about it. Is AnotherView a function? How do I call AnotherView? I have no experience with binding. I can't follow what you wrote. Can you rewrite what you wrote with more detail? Please help.

Did you try with the simple text1 ?

As you keep posting only bits and pieces, it is impossible to understand what you are doing.

 

AnotherView a function?

No, it is a View, like ViewDetail

 

Let me explain. The user(driver) enters the cost of the delivery and the amount he got paid. The program(enterp) figures out the tip, tip %, average tip, total costs, total tips, total tip % and total cash and writes it out to the screen. Using detailLine I want to print all deliveries(up to 44) and tips to the second screen. I hope this helps.

No, it does not help. What would help would be to see the complete main view, with all the var declarations, all the func inside.

 

Can you rewrite what you wrote with more detail?

No as long as you do not provide complete code as requested several time.

We see nowhere a call to display ViewDetail. Where is it ?

A last advice: you should start by learning SwiftUI and Swift. Looks like you are rushing to coding. That's a dead end.

cannot find in scope
 
 
Q