[Solved] How do you make an equation in swift with multiple variables? [closed]


There are a few errors I see:

  1. You need a space in /1005.967 after the / operator — there are likely other spots like this as well.
  2. Near the end of the expression, you have a floating sin that doesn’t have an operator before it and isn’t followed with parenthesis at all
  3. There are many spots where you refer to just d, but probably mean calculation.d
  4. You have a mismatched number of open and closed parenthesis.
  5. You don’t actually have a Calculation property defined on your view, so there’s nothing to do the calculations on yet.

Because I don’t know the intent of some of this stuff, I can’t actually fix it for you. But, I’d recommend trying to clean up the code a little — at the least, it’ll make it easier to debug.

To start, move this out of the interpolated String and into a computed property:

var calc : Int { //Int? -- see comment about this
  //your calculation here
} 

var body: some View {
  Text("\(calc)m")
}

Then, I’d break the calculation up into much smaller expressions that are more readable and would let you find your errors more easily than trying to sift through so many parenthesis, etc. Even a computer can theoretically handle a long, tough-to-read expression, it makes it a challenging debugging issue for us humans.

I’d also be really surprised if you truly want Int for these properties. You have a bunch of spots where you’re doing things like Int(0.98), which doesn’t make sense, because it’ll get rounded to 1. Perhaps you instead want to use Double or Float for everything? You’ll see as you start breaking the instructions up and the compiler starts to find more errors once it can parse everything correctly that you’re going to end up with type mismatches between things like Int and Double in the existing expressions.

4

solved How do you make an equation in swift with multiple variables? [closed]