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.

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)
}
I don't know how to do this.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.

Please help!!

@robert0605

Please, STOP mixing all questions. You return to an older part of the post when you have full code available.

Do you understand the answers you receive or even read them ?

So, just one point left:

  • Does the code you received in the last posts work ? (It works for me, it should for you)
  • Then, close the the thread by marking the correct answer.

Yes I read all answers but I don't understand them. I didn't try the code I received in the last posts. I don't know how to try.

Can you explain this please.

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"

cannot find in scope
 
 
Q