Error

Hello, I am making an app for Apple watch but I have lots of errors.

My code is:


import SwiftUI



struct ContentView: View {

    

    @State private var currentCount = UserDefaults.standard.integer(forKey: "currentCount")

    

   @State private var isDecrementOn = false

    

    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)

                

                func performMaths(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")

               

                struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}





    }

}

The first error is unterminated string literal on:  Image(systemName: isDecrementOn ? "minus" : plus")  
The second error is expected separator for:  .foregroundColor(.blue) but when I click fix the same error pops up somewhere else on the code.
Thats the same error on here.

 ScrollView(.horizontal){

            HStack(alignment: .center, spacing: 10){

                Button(action: {

                    self.performsMath(isSubtract: self.isDecrementOn, num: 1)

                

                } <- this line

                Text(isDecrementOn ? "-1" : "+1")




The last errors are:

Expected '}' at end of brace statement
Expected '}' at end of closure
Expected '}' in struct

and they are at the end of my code but when I do what they ask the errors keep ******* up on the line underneath.

Could someone help me?

Thanks
Answered by srt818071 in 658041022
Thank you! That worked any thanks for the tips for pasting. I have 2 more errors now.

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions.

Do you know what that means and how to fix it? It is on this line: var body: some View
in the beginning.

Also, Expected expression in list of expressions and Expected expression. What does that mean?
Have you looked at your post ? It is just unreadable with all those extra blank lines.

When you paste code, please:
  • use Paste and Match Style

  • format with code formatter tool (<>)

And there misses lines, so it is impossible to understand your code.
There are also very basic errors:
Code Block
            Image(systemName: isDecrementOn ? "minus" : plus")

Misses a quote before plus"
you define func performMaths in the middle of the code. and the func misses 3 } to end properly !
All buttons actions mis a closing ) for the action, and the Text() must be inside { } as a closure
You define performMaths but call performsMat
You define 2 scrollViews. Are they inside each other ?
It is a real mess.

Please, make the effort to post something readable.

I did a little formatting ans some corrections:

Code Block
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)
}
}
// THERE IS A PROBLEM HERE: WHERE IS THIS SCROLLVIEW ? INSIDE VSTACK ?
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()
}
}
}
}



Accepted Answer
Thank you! That worked any thanks for the tips for pasting. I have 2 more errors now.

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions.

Do you know what that means and how to fix it? It is on this line: var body: some View
in the beginning.

Also, Expected expression in list of expressions and Expected expression. What does that mean?
Hi, I have to many errors but I was wondering if someone could help fix my code.

Thanks






Code Block import SwiftUI
struct ContentView: View {
    
   
    @State private var currentCount = UserDefaults.standard.integer(forKey: "currentCount")
    
   @State private var isDecrementOn = false
    
    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)
    }
}
    func ScrollView(.horizontal){
            HStack(alignment: .center, spacing: 10){
                Button(Text(isDecrementOn ? "-1" : "+1"), action: {
                    self.performsMath(isSubtract: self.isDecrementOn, num: 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)
                
                func performMaths(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")
               
                struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
    }
}
              
                }}}
}


Error
 
 
Q