CopyPastor

Detecting plagiarism made easy.

Score: 0.8033313155174255; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2023-10-02
by Jaspalsinh Gohil

Original Post

Original - Posted on 2020-06-12
by Peter Kreinz



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Here is an example of how to use the theIncrement() function to increment aCount in the inner ForEach loop:
struct ContentView: View { @State var gridSize = 3 @State var selected = 0 @State var aCount = 0 func theIncrement(theCount: Int) -> Int { let theCounter = theCount + 1 return theCounter } var body: some View { Grid (horizontalSpacing: 0, verticalSpacing: 0) { ForEach(0..<gridSize, id: \.self) { row in GridRow{ ForEach(0..<gridSize, id: \.self) { col in Rectangle() .foregroundColor(.gray) .overlay(Text("\(aCount)")) .border(Color.black) .bold() // Increment aCount and assign the new value to a temporary variable let newCount = theIncrement(theCount: aCount) // Update aCount with the new value aCount = newCount } } } } } }
The advantage of the following approach is that the views in ForEach even change if state values ​​change:
struct ContentView: View { @State private var array = [1, 2, 3] func doSomething(index: Int) { self.array[index] = Int.random(in: 1..<100) } var body: some View { let arrayIndexed = array.enumerated().map({ $0 }) return List(arrayIndexed, id: \.element) { index, item in Text("\(item)") .padding(20) .background(Color.green) .onTapGesture { self.doSomething(index: index) } } } }
> ... this can also be used, for example, to remove the last divider > in a list:
struct ContentView: View { init() { UITableView.appearance().separatorStyle = .none } var body: some View { let arrayIndexed = [Int](1...5).enumerated().map({ $0 }) return List(arrayIndexed, id: \.element) { index, number in VStack(alignment: .leading) { Text("\(number)") if index < arrayIndexed.count - 1 { Divider() } } } } }

        
Present in both answers; Present only in the new answer; Present only in the old answer;