Multiplying ints in swift

让人想犯罪 __ 提交于 2019-12-24 13:50:30

问题


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!


回答1:


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




回答2:


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?



来源:https://stackoverflow.com/questions/27723349/multiplying-ints-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!