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")
}
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.

Can I send you the whole program? Or should I post it here?

I'm going to post the whole program in 2 or 3 parts.

Part 1.

import SwiftUI
struct ContentView: View {
    @State var tipa: [Double] = []
    @State var costa: [Double] = []
    @State private var cost = ""
    @State private var costt  =  Double?(0)
    @State private var paidt  =  Double?(0)
    @State private var paid = ""
    @State var tipp = ""
    @State var tipc = ""
    @State var tipaa = ""
    @State var totalcost = ""
    @State var totaltips = ""
    @State var totalcash = ""
    @State var tipcc = ""
    @State var numbc = ""
    @State var deletep  = 0
    @State var number = 0
    @State var msg = "Enter cost and how much was paid.  Enter numbers"
    @State var msg2 = " and one . decimal point only.  Then press enter."
    @State var emsg = ""
    
    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 )
                    }
                    NavigationLink(destination: viewdetail()) {
                        Text("Detail")
                            .frame(width: 100, height: 50, alignment: .center)
                            .background(Color.white)
                            .foregroundColor(.blue)
                            .font(.largeTitle)
                    }
                    Button {
                        delete(costa:&costa,tipa:&tipa, number: &number)
                    }
                    label: { Text("Delete")
                            .font(.largeTitle)
                        .foregroundColor(.red) }
                }
                VStack {
                    Spacer()
                    HStack
                    {
                        Spacer()
                        Text("Avg Tip")
                            .font(.title)
                        Spacer()
                        Spacer()
                        Text("    Tip %")
                            .font(.title)
                        Spacer()
                        Text("      #")
                            .font(.title)
                        Spacer()
                    }
                    HStack {
                        Spacer()
                        Spacer()
                        Spacer()
                        Text(tipaa)
                            .frame(width: 145, height: 25)
                            .fontWeight(.bold)
                            .font(.system(size: 22))
                            .foregroundColor(.blue)
                        Spacer()
                        Spacer()
                        Text(tipcc)
                            .frame(width: 120, height: 5)
                            .padding(10)
                            .fontWeight(.bold)
                            .font(.system(size: 22))
                            .foregroundColor(.blue)
                        Spacer()
                        Text(numbc)
                            .frame(width: 55, height: 5)
                            .padding(10)
                            .fontWeight(.bold)
                            .font(.system(size: 22))
                            .foregroundColor(.blue)
                        Spacer()
                    }
                }

Part 2.

  VStack {
                    Spacer()
                    HStack
                    {
                        Text("Total")
                            .font(.title)
                        Spacer()
                        Text("Total")
                            .font(.title)
                        Spacer()
                        Text("Total ")
                            .font(.title)
                    }
                }
                VStack {
                    HStack
                    {
                        Text("Cost")
                            .font(.title)
                        Spacer()
                        Text("Tips")
                            .font(.title)
                        Spacer()
                        Text("Cash")
                            .font(.title)
                    }
                }
                HStack {
                    Text(totalcost)
                        .frame(width: 145, height: 25)
                        .fontWeight(.bold)
                        .font(.system(size: 22))
                        .foregroundColor(.blue)
                    Spacer()
                    Text(totaltips)
                        .frame(width: 145, height: 25)
                        .fontWeight(.bold)
                        .font(.system(size: 22))
                        .foregroundColor(.blue)
                    Spacer()
                    Text(totalcash)
                        .frame(width: 165, height: 25)
                        .fontWeight(.bold)
                        .font(.system(size: 22))
                        .foregroundColor(.blue)
                    
                }
                Spacer()
            } }
    }
    func enterp()
    {
        @Binding var text1: String
        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)     }
    }  }

#Preview {
    ContentView()
}

Part 3 viewdetail.swift

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"
            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()
}

Don't post the whole program. Aside from the fact it's far too much code and you'll end up posting it over numerous posts, you still haven't mastered the use of the code formatting tool so we won't be able to understand it anyway.

Please just reduce your code to the smallest bit possible - remove any formatting that's unnecessary, like fonts and padding etc. - you can add it back later.

As @Claude31 was saying, use a Binding to pass values from one View to another, something like this:

struct ViewOne: View {
  @State private var firstNames: [String] = ["Dave", "Geoff", "Bob", "Charlie"]
  var body: some View {
    Text("Some text")
    ViewTwo(firstNames: $firstNames)
  }
}

struct ViewTwo: View {
  @Binding firstNames: [String]
  var body: some View {
    firstNames.forEach { name in
      Text("Current name = '\(name)'")
    }
  }
}

That should display this:

Some text
Current name = 'Dave'
Current name = 'Geoff'
Current name = 'Bob'
Current name = 'Charlie'

Okay, well, you went ahead anyway. Here's my update:

//  ContentView.swift

import SwiftUI

struct ContentView: View {
	@State private var tipa: [Double] = []
	@State private var costa: [Double] = []
	@State private var cost = ""
	@State private var costt: Double? = 0.0
	@State private var paidt: Double? = 0.0
	@State private var paid = ""
	@State private var tipp = ""
	@State private var tipc = ""
	@State private var tipaa = ""
	@State private var totalCost = ""
	@State private var totalTips = ""
	@State private var totalCash = ""
	@State private var tipcc = ""
	@State private var numbc = ""
	@State private var deletep = 0
	@State private var number = 0
	@State private var errorMsg = ""

	var body: some View {
		GeometryReader { g in
			NavigationStack {
				VStack {
					HStack {
						// No need to manually pad the date out with spaces, which will never be correct.
						// Just use an HStack and put a Spacer() in, so it goes: |<.....Spacer().....>Date|
						Spacer()
						Text(Date().formatted(date: .numeric, time: .omitted))
							.foregroundStyle(Color.black)
							.fontWeight(.bold)
							.padding(.trailing, 10)
					}

					Text("Driver's Food Delivery")
						.font(.largeTitle)
						.fontWeight(.bold)

					Image("Screenshot 2024")

					VStack {
						Spacer()
						Text(errorMsg)
							.foregroundStyle(Color.red)
						// Here, put the text in one Text element, and tell SwiftUI to put it on two lines.
						// No need for msg/msg2 variables.
						Text("Enter cost and how much was paid. Enter numbers and one decimal point only, then press Enter.")
							.foregroundStyle(Color.blue)
							.lineLimit(2, reservesSpace: true)
						Spacer()
					}

					// Use a Grid to lay out your data; much cleaner, and SwiftUI will keep it in columns for you
					Grid(alignment: .center, horizontalSpacing: 6, verticalSpacing: 6) {
						GridRow {
							Text("Cost")
								.gridColumnAlignment(.leading)
							Text("Paid")
								.gridColumnAlignment(.leading)
							Text("Tip")
								.gridColumnAlignment(.trailing)
							Text("Tip %")
								.gridColumnAlignment(.trailing)
						}
						.font(.headline)

						GridRow {
							TextField("Cost", text: $cost)
								.padding(10)
								.background(Color.brown)
								.cornerRadius(12)
								.frame(width: g.size.width * 0.2)
								.gridColumnAlignment(.leading)

							TextField("Paid", text: $paid)
								.padding(10)
								.background(Color.brown)
								.cornerRadius(12)
								.frame(width: g.size.width * 0.2)
								.gridColumnAlignment(.leading)

							Text(tipp)
								.frame(width: g.size.width * 0.2)
								.gridColumnAlignment(.trailing)
							Text(tipc)
								.frame(width: g.size.width * 0.2)
								.gridColumnAlignment(.trailing)
						}
					}
					.padding(.vertical, 20)

					HStack {
						Button {
							enterPayment()
						} label: {
							Text("Enter")
								.font(.title2)
								.padding(10)
								.background(Color.green)
								.foregroundStyle(Color.white)
								.cornerRadius(12)
						}
						.padding(.leading, 10) // Keeps it off the edge of the screen

						NavigationLink(destination: ViewDetail()) {
							Text("Detail")
								.font(.title2)
								.padding(10)
								.background(Color.blue)
								.foregroundStyle(Color.white)
								.cornerRadius(12)
						}

						Spacer() // Leaves a gap between the Enter & Detail buttons, and the destructive Delete button
						Button {
							delete(costa: &costa, tipa: &tipa, number: &number)
						} label: {
							Text("Delete")
								.font(.title2)
								.padding(10)
								.background(Color.red)
								.foregroundStyle(Color.white)
								.cornerRadius(12)
						}
						.padding(.trailing, 10) // Keeps it off the edge of the screen
					}

					Divider()  // Nice little divider
						.padding(.vertical, 20)
				}

				VStack {
					Grid(alignment: .center, horizontalSpacing: 6, verticalSpacing: 6) {
						GridRow {
							Text("Avg. Tip")
								.padding(.horizontal, 20)
							Text("Tip %")
								.padding(.horizontal, 20)
							Text("#")
								.padding(.horizontal, 20)
						}
						.gridColumnAlignment(.center)
						.font(.title3)
						GridRow {
							Text(tipaa)
								.padding(.horizontal, 20)
							Text(tipcc)
								.padding(.horizontal, 20)
							Text(numbc)
								.padding(.horizontal, 20)
						}
						.gridColumnAlignment(.center)
						.font(.system(size: 22, weight: .bold))
						.foregroundStyle(Color.blue)
					}
					Spacer()
				}

				VStack {
					Grid(alignment: .center, horizontalSpacing: 6, verticalSpacing: 6) {
						GridRow {
							Text("Total Cost")
								.padding(.horizontal, 10)
							Text("Total Tips")
								.padding(.horizontal, 10)
							Text("Total Cash")
								.padding(.horizontal, 10)
						}
						.gridColumnAlignment(.center)
						.font(.title3)
						GridRow {
							Text(totalCost)
								.padding(.horizontal, 10)
							Text(totalTips)
								.padding(.horizontal, 10)
							Text(totalCash)
								.padding(.horizontal, 10)
						}
						.gridColumnAlignment(.center)
						.font(.system(size: 22, weight: .bold))
						.foregroundStyle(Color.blue)
					}
					Spacer()
				}
			}
		}
	}

	func enterPayment() {
		var ttips = 0.0
		var tcost = 0.0
		var tipo = 0.00
		costt = Double(cost)
		paidt = Double(paid)
		errorMsg = ""
		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, +))
			tipaa = (ttips / Double(number)).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)
			tipc = String(format: "%3.0f%%", Double(tipc)!)
			tipcc = String(ttips / tcost * 100)
			tipcc = String(format: "%3.0f%%", Double(tipcc)!)
			totalCash = (tcost + ttips).formatted(.currency(code: "USD"))

		} else {
			errorMsg = "Enter numbers and 1 decimal point only."
		}
	}
}

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"
		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 {
    ContentView()
}

Fist problem is that

    func detailLine(costa: inout [Double], tipa: inout [Double]) {

is never called

  • how should it get costa and tips ?
  • When do you want to call ?
  • When you tap Detail ?

If so, here is the fix, but I do not understand the logic here:

struct ViewDetail: View {
    @State var text1: String = ""
    @State var tip1: String = ""
    @State var text23: String = ""
    @State var tip23: String = ""
    
    var body: some View {
        HStack {
            Text(text1)
            Text(tip1)
            Text(text23)
            Text(tip23)
        }
        .onAppear {
            text1 = "125"
        }
    }
    
    func detailLine(costa: inout [Double], tipa: inout [Double]) {  // NEVER Called
        print(costa, tipa)
        text1 = "125"
        print("detail")
    }
}

Second problem

  • How do you want to use text1 in ContentView ? It never appears.
  • If you want, here is how to do with Binding

Thrid problem in:

func delete(costa: inout [Double], tipa: inout [Double], number: inout Int) {
    print(costa, tipa)
    tipa.removeLast()   // Crash here as tipa is empty

There is a simple fix:

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

The complete code in next post.

So the complete code part1:

struct ContentView: View {
    @State var text1: String = ""   // <<-- Declare HERE ; could be an array of String
    @State private var tipa: [Double] = []
    @State private var costa: [Double] = []
    @State private var cost = ""
    @State private var costt: Double? = 0.0
    @State private var paidt: Double? = 0.0
    @State private var paid = ""
    @State private var tipp = ""
    @State private var tipc = ""
    @State private var tipaa = ""
    @State private var totalCost = ""
    @State private var totalTips = ""
    @State private var totalCash = ""
    @State private var tipcc = ""
    @State private var numbc = ""
    @State private var deletep = 0
    @State private var number = 0
    @State private var errorMsg = ""
    
    var body: some View {
        GeometryReader { g in
            NavigationStack {
                VStack {
                    HStack {
                        // No need to manually pad the date out with spaces, which will never be correct.
                        // Just use an HStack and put a Spacer() in, so it goes: |<.....Spacer().....>Date|
                        Spacer()
                        Text(Date().formatted(date: .numeric, time: .omitted))
                            .foregroundStyle(Color.black)
                            .fontWeight(.bold)
                            .padding(.trailing, 10)
                    }
                    
                    Text("Driver's Food Delivery")
                        .font(.largeTitle)
                        .fontWeight(.bold)
                    
                    Image("Screenshot 2024")
                    
                    VStack {
                        Spacer()
                        Text(errorMsg)
                            .foregroundStyle(Color.red)
                        // Here, put the text in one Text element, and tell SwiftUI to put it on two lines.
                        // No need for msg/msg2 variables.
                        Text("Enter cost and how much was paid. Enter numbers and one decimal point only, then press Enter.")
                            .foregroundStyle(Color.blue)
                            .lineLimit(2, reservesSpace: true)
                        Spacer()
                    }
                    
                    // Use a Grid to lay out your data; much cleaner, and SwiftUI will keep it in columns for you
                    Grid(alignment: .center, horizontalSpacing: 6, verticalSpacing: 6) {
                        GridRow {
                            Text("Cost")
                                .gridColumnAlignment(.leading)
                            Text("Paid")
                                .gridColumnAlignment(.leading)
                            Text("Tip")
                                .gridColumnAlignment(.trailing)
                            Text("Tip %")
                                .gridColumnAlignment(.trailing)
                        }
                        .font(.headline)
                        
                        GridRow {
                            TextField("Cost", text: $cost)
                                .padding(10)
                                .background(Color.brown)
                                .cornerRadius(12)
                                .frame(width: g.size.width * 0.2)
                                .gridColumnAlignment(.leading)
                            
                            TextField("Paid", text: $paid)
                                .padding(10)
                                .background(Color.brown)
                                .cornerRadius(12)
                                .frame(width: g.size.width * 0.2)
                                .gridColumnAlignment(.leading)
                            
                            Text(tipp)
                                .frame(width: g.size.width * 0.2)
                                .gridColumnAlignment(.trailing)
                            Text(tipc)
                                .frame(width: g.size.width * 0.2)
                                .gridColumnAlignment(.trailing)
                        }
                    }
                    .padding(.vertical, 20)
                    
                    HStack {
                        Button {
                            enterPayment()
                        } label: {
                            Text("Enter")
                                .font(.title2)
                                .padding(10)
                                .background(Color.green)
                                .foregroundStyle(Color.white)
                                .cornerRadius(12)
                        }
                        .padding(.leading, 10) // Keeps it off the edge of the screen
                        
                        NavigationLink(destination: ViewDetail(text1InDetail: $text1)) {
                            Text("Detail")
                                .font(.title2)
                                .padding(10)
                                .background(Color.blue)
                                .foregroundStyle(Color.white)
                                .cornerRadius(12)
                        }
                        

End of code

                        Spacer() // Leaves a gap between the Enter & Detail buttons, and the destructive Delete button
                        Button {
                            delete(costa: &costa, tipa: &tipa, number: &number)
                        } label: {
                            Text("Delete")
                                .font(.title2)
                                .padding(10)
                                .background(Color.red)
                                .foregroundStyle(Color.white)
                                .cornerRadius(12)
                        }
                        .padding(.trailing, 10) // Keeps it off the edge of the screen
                    }
                    Text(text1)  // Just to show it is updated DO NOT KEEP THIS
                    
                    Divider()  // Nice little divider
                        .padding(.vertical, 20)
                }
                
                VStack {
                    Grid(alignment: .center, horizontalSpacing: 6, verticalSpacing: 6) {
                        GridRow {
                            Text("Avg. Tip")
                                .padding(.horizontal, 20)
                            Text("Tip %")
                                .padding(.horizontal, 20)
                            Text("#")
                                .padding(.horizontal, 20)
                        }
                        .gridColumnAlignment(.center)
                        .font(.title3)
                        GridRow {
                            Text(tipaa)
                                .padding(.horizontal, 20)
                            Text(tipcc)
                                .padding(.horizontal, 20)
                            Text(numbc)
                                .padding(.horizontal, 20)
                        }
                        .gridColumnAlignment(.center)
                        .font(.system(size: 22, weight: .bold))
                        .foregroundStyle(Color.blue)
                    }
                    Spacer()
                }
                
                VStack {
                    Grid(alignment: .center, horizontalSpacing: 6, verticalSpacing: 6) {
                        GridRow {
                            Text("Total Cost")
                                .padding(.horizontal, 10)
                            Text("Total Tips")
                                .padding(.horizontal, 10)
                            Text("Total Cash")
                                .padding(.horizontal, 10)
                        }
                        .gridColumnAlignment(.center)
                        .font(.title3)
                        GridRow {
                            Text(totalCost)
                                .padding(.horizontal, 10)
                            Text(totalTips)
                                .padding(.horizontal, 10)
                            Text(totalCash)
                                .padding(.horizontal, 10)
                        }
                        .gridColumnAlignment(.center)
                        .font(.system(size: 22, weight: .bold))
                        .foregroundStyle(Color.blue)
                    }
                    Spacer()
                }
            }
        }
    }
    
    func enterPayment() {
        var ttips = 0.0
        var tcost = 0.0
        var tipo = 0.00
        costt = Double(cost)
        paidt = Double(paid)
        errorMsg = ""
        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, +))
            tipaa = (ttips / Double(number)).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)
            tipc = String(format: "%3.0f%%", Double(tipc)!)
            tipcc = String(ttips / tcost * 100)
            tipcc = String(format: "%3.0f%%", Double(tipcc)!)
            totalCash = (tcost + ttips).formatted(.currency(code: "USD"))
            
        } else {
            errorMsg = "Enter numbers and 1 decimal point only."
        }
    }
}

struct ViewDetail: View {
    @Binding var text1InDetail: String // Binding here = "" Could be an Array of 44 String
    @State var tip1: String = ""
    @State var text23: String = ""
    @State var tip23: String = ""
    
    var body: some View {
        HStack {
            Text(text1InDetail)
            Text(tip1)
            Text(text23)
            Text(tip23)
        }
        .onAppear {
            text1InDetail = "125"// If array: text1InDetail[0] = 125
        }
    }
    
    func detailLine(costa: inout [Double], tipa: inout [Double]) {  // NEVER Called
        print(costa, tipa)
        text1InDetail = "125"
        print("detail")
    }
}

func delete(costa: inout [Double], tipa: inout [Double], number: inout Int) {
    print(costa, tipa)
    if !tipa.isEmpty && !costa.isEmpty {
        tipa.removeLast()
        costa.removeLast()
        number -= 1
    }
    print(costa,tipa)
}
cannot find in scope
 
 
Q