swift example -- operational error

Asked 2 years ago, Updated 2 years ago, 74 views

var x = 10
for index in 1 ... 20 {
    let y = index * x--
}

I'm studying using iPad Pro's Playground. I played the same chord as the example Unary operator '--' cannot be applied to operands of type '@lvalueInt'. That's what it says. Please let me know the solution.

swift-playground swift

2022-09-21 17:15

1 Answers

In Swift3, the -- operator and the ++ operator are missingYes, I think you copied the old example. We solved it using the -+ operator.

var x = 10
for index in 1 ... 20 {
    x -= 1
    let y = index * x
    print(y)
}

Press the [Do] button to press the next code.

9
16
21
24
25
24
21
16
9
0
-11
-24
-39
-56
-75
-96
-119
-144
-171
-200

Is it correct if it's printed out like this?


2022-09-21 17:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.