-1

I'm creating a math app and am trying to assign the correct answer to a variable. I've tried these two options below

@State private var correctAnswer1: Int {
        number1L1 + number2L1
    }

With this, I receive the error "Property wrapper cannot be applied to a computed property".

 if currentLevel == 1
            {
                Text("   \(number1L1)")
                    .font(.system(size:70))
                Text("+ \(number2L1)")
                    .font(.system(size:70))
                Text("____")
                    .font(.system(size:70))

               correctAnswer = number1L1 + number2L1
                
            }

With this I receive the error "Type () cannot conform to 'View'"

How do I assign this variable without running into an error?

4
  • Learn how to separate data from the view. In SwiftUI the view should reflect the state of the view model. Also it passes the actions to the view model in order to modify the data. See my answer here: stackoverflow.com/questions/73860747/… Commented Nov 16, 2022 at 3:25
  • Comment out correctAnswer = number1L1 + number2L1. Remove @State from your first snippet. Then, everything should run. Commented Nov 16, 2022 at 3:30
  • You cannot have "normal" code in the body of a View like your correctAnswer = number1L1 + number2L1. In a View you should have other Views, this is what the error is telling you. From the code you show, it is clear you need to brush-up on the basics of SwiftUI , see for example: developer.apple.com/tutorials/swiftui To avoid the error, put that code inside a function. Commented Nov 16, 2022 at 3:57
  • 1
    Does this answer your question? SwiftUI Instantiate a variable using another variable Commented Nov 16, 2022 at 4:05

1 Answer 1

1

You really have two questions here. In the first case you have a property that is a computed property.

private var correctAnswer1: Int {
        number1L1 + number2L1
    }

Says that every time you ask for yourObject.correctAnswer1 the system should compute its value by running the code in the block.

Something a property that is attributed with @State, in contrast, is a box that holds a single value. You don't have a single value, you have a mechanism for computing a value. The compiler complains.

I the second case, you're laying out a View.

When you are working inside the body block of a SwiftUI View you are in a special environment called a ViewBuilder. Within that environment, each expression is expected to return some kind of View. The compiler collects them in a special way and will combine them together to make a user interface. (The environment also allows you some control-flow structures like if statements that are treated specially. If you are really curious you can search the web for information about Swift Result Builders)

In your case, you've included an expression that doesn't return a View in a place where Swift expects each expression to return one and Swift complains.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.