Monday, March 27, 2023
HomeiOS Developmentios - Passing a variable to a subview with a for loop...

ios – Passing a variable to a subview with a for loop crashes app in SwiftUI


I’ve not too long ago bought into iOS improvement utilizing SwiftUI over the previous week. I am engaged on an app which has a most important ContentView, containing a scrollview, which then calls one other view when the rect is tapped. This view then calls one other view in its scrollview to show a progress bar managed by a dial.

Job Def:

struct Job: Identifiable {
    var id = UUID()
    var title: String
    var subtitle: String
    var picture: String
    var colours: [Color]
    var funds: CGFloat
    
}

So ContentView -> if (present) {ForEach(information){ information in SubView1(information)}}

struct ContentView: View {
    @Namespace var namespace
    @State var present = false
    @State var selectedID = UUID()
    
    var physique: some View {
        ZStack{
            ScrollView{
                if !present{
                    ForEach(duties) { process in
                        TaskComponent(namespace: namespace, present: $present, process: process)
                            .onTapGesture {
                                withAnimation(.spring(response: 0.6, dampingFraction: 0.8)){
                                    present.toggle()
                                    selectedID = process.id
                                }
                            }
                    }
                } else {
                    ForEach(duties) { process in
                        Rectangle()
                            .fill(Coloration.init(.displayP3, pink: 22/255, inexperienced: 28/255, blue: 34/255, opacity: 1))
                            .body(peak: 300)
                            .cornerRadius(30)
                        .padding(20)
                    }
                }
            }
  
            if present{
                ForEach(duties) { process in
                    if process.id == selectedID {
                        TaskView(namespace: namespace, present: $present, process: process)
                            .zIndex(1)
                    }
                    
                }
            }
        }
        
    }
    
}

SubView1 -> VStack { blah blah .. SubView2}

struct TaskView: View {
      var physique: some View {
          VStack{
            let cnt = process.funds

            // MARK: THIS IS THE ISSUE LINE
            // utilizing cnt as an alternative of depend causes a crash
            DialView(colours: process.colours, depend: 8)
                .padding(10)
        }
      }
}

SubView2 -> struct: Form { func: -> Path { for i in …depend{ do stuff } }

struct DialView: View {
    @State var depend: CGFloat = 12
    
    var physique: some View {
        VStack {
            GeometryReader { bounds in
                VStack(spacing: 70){
                    BudgetItem(depend: Int(depend))
                }
            }
         }

struct BudgetItem: Form {
        let depend: Int
        
        func path(in rect: CGRect) -> Path {
            var path = Path()
            
            path.transfer(to: CGPoint(x: 0, y: rect.midY))
            
            var maxX: CGFloat = 0
            let remainingSpace: CGFloat = rect.width - (CGFloat(depend)*10*2)
            let lineLength: CGFloat = remainingSpace / CGFloat(depend - 1)
            
            for i in 1...depend {
                path.addRect(CGRect(origin: CGPoint(x: maxX, y: rect.midY - 10), measurement: CGSize(width: 20, peak: 20)))
                maxX += 20
                path.transfer(to: CGPoint(x: maxX, y: rect.midY))
                if i != depend {
                    maxX += lineLength
                    path.addLine(to: CGPoint(x: maxX, y: rect.midY))
                }
            }
            return path
        }
    }

}

So a subview has a variable depend which controls what number of shapes get displayed. When it is a CGFloat hardcoded, it really works high quality. Nonetheless, when I attempt to have the depend distinctive to every information entry (ie information.depend) it crashes.

Any insights can be appreciated and I can add extra concrete code as wanted.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments