user3369038
user3369038

Reputation: 119

Multiplying ints in swift

I am new to swift and iOS programming and learning slowly here. So I have:

@IBOutlet weak var timeEntered: UITextField!



@IBAction func countDown(sender: AnyObject)
    {
        var total = timeEntered.text.toInt()

        total = total *7


    }

The line total = total *7 gives me the error "Consecutive statements on a line must be separated by a ;"

I read here that you should do ( I don't know why but...)

total = total! *7

And still I get the same error. Any help would be highly appreciated. Thanks!

Upvotes: 1

Views: 1689

Answers (2)

gnasher729
gnasher729

Reputation: 52538

In Swift, operators like "*" must have either no space on either side of the operator, or white space on both sides of the operator.

That forces you to avoid ugliness like your

total = total *7

Write it nicely. Either of these:

total = total * 7
total = total*7

Now this:

total = total! *7

That's just grabbing around in the dark without any idea what you are doing. That's no way how to program. You must know why. How can you ever be sure what your code does when you don't know the why's?

Upvotes: 1

Maxime Defauw
Maxime Defauw

Reputation: 190

I think that you must separate the '*' and the '7'. Try this total = total * 7

Upvotes: 1

Related Questions