3 code errors

Hi,

I am a new coder and I have 3 code errors in my app that I am making.

Which are
  1. Value of type 'String' has no member 'frame' : line 174

  2. Cannot infer contextual base in reference to member 'all': line 176

  3. Closure containing a declaration cannot be used with function builder 'ViewBuilder' : line 186

Finally, here is my code:

Code Block import SwiftUI
struct ContentView: View {
    @State private var currentCount = UserDefaults.standard.integer(forKey: "currentCount")
    @State private var isDecrementOn = false
    
    func performsMath(isSubtract: Bool, num: Int) {
        var result = self.currentCount
        
        if isSubtract==true{
            result -= num
        } else {
            result += num
            if result >= 0 {
                currentCount = result
                UserDefaults.standard.set(currentCount, forKey: "currentCount")
            }
        }
    }
    var body: some View {
        ScrollView {
            VStack(alignment: .center, spacing: 10) {
                HStack(alignment: .center, spacing: 20) {
                    Button(action: {
                        self.isDecrementOn.toggle()
                    }) {
                        Image(systemName: isDecrementOn ? "minus" : "plus")
                            .font(.headline)
                            .foregroundColor(.blue)
                        aspectRatio(contentMode: .fit)
                    }
                    
                    Button(action: {
                        self.currentCount = 0
                        UserDefaults.standard.set(0,forKey: "currentCount")
                    }) {
                        
                        Image(systemName: "trash")
                            .font(.headline)
                            .foregroundColor(.red)
                        aspectRatio(contentMode: .fit)
                    }
                }
                
                Button(action: {
                    self.performsMath(isSubtract: self.isDecrementOn, num: 1)
                }){
                
                Text("\(self.currentCount)")
                    .font(.title)
                }
            }
                    
            ScrollView(.horizontal) {
            
            HStack(alignment: .center, spacing: 10) {
                Button(action: {
                    self.performsMath(isSubtract: self.isDecrementOn, num: 1)
                }) {
                    Text(isDecrementOn ? "-1" : "+1")
                }
                Button(action: {
                    self.performsMath(isSubtract: self.isDecrementOn, num: 2)
                }) {
                    Text(isDecrementOn ? "-2" : "+2"
                  )  }
                Button(action: {
                self.performsMath(isSubtract: self.isDecrementOn, num: 3)
                }) {
                Text(isDecrementOn ? "-3" : "+3"
               ) }
                
                Button(action: {
                self.performsMath(isSubtract: self.isDecrementOn, num: 4)
                }) {
                Text(isDecrementOn ? "-4" : "+4"
               ) }
                Button(action: {
                self.performsMath(isSubtract: self.isDecrementOn, num: 5)
                }) {
                Text(isDecrementOn ? "-5" : "+5"
              )  }
                Button(action: {
                self.performsMath(isSubtract: self.isDecrementOn, num: 10)
                }) {
                Text(isDecrementOn ? "-10" : "+10"
                        
                    .frame(width: 50)
.padding(.all)
                        ) }
                    }
                        
                        struct ContentView_Previews: PreviewProvider {
            static var previews: some View {
                ContentView()   }
                }
            }
        }
    }}






Could someone help me?
You should better keep your code properly indented.
Cmd-A Ctrl-I

That will reveal some simple mistakes.


Value of type 'String' has no member 'frame' : line 174

Cannot infer contextual base in reference to member 'all': line 176

Your line 170...180 will look like this when extra whitespaces and newlines removed:
Code Block
Text(isDecrementOn ? "-10" : "+10".frame(width: 50).padding(.all) )

You write .frame(width: 50) immediately after a String literal "+10".
You may want to add .frame to Text, not a String.


Closure containing a declaration cannot be used with function builder 'ViewBuilder' : line 186

The line struct ContentView_Previews: PreviewProvider { needs to be placed outside of the definition of struct ContentView.
Meaning you need to write it after the closing brace } matching the opening brace struct ContentView: View {.

You are writing struct ContentView_Previews: PreviewProvider { just after the closing brace for HStack(alignment: .center, spacing: 10) {.


You need to be careful enough about matching parentheses.
3 code errors
 
 
Q