本文介绍了Swift中的分割操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我经常出错?

 var rotation:Float= Double(arc4random_uniform(50))/ Double(100-0.2)

实际上我也尝试过这个:

Actually i try this one too:

 var rotation:Double= Double(arc4random_uniform(50))/ Double(100-0.2)

谢谢

推荐答案

Swift具有严格限制运算符周围空白的规则.除'/'是二进制运算符.

Swift has strict rules about the whitespace around operators. Divide '/' is a binary operator.

重要规则是:

这意味着您需要在/之前添加一个空格或在其后删除该空格以表明它是二进制运算符:

That means that you need to add a space before the / or remove the space after it to indicate that it is a binary operator:

var rotation = Double(arc4random_uniform(50)) / (100.0 - 0.2)

如果您希望rotation成为Float,则应使用它而不是Double:

If you want rotation to be a Float, you should use that instead of Double:

var rotation = Float(arc4random_uniform(50)) / (100.0 - 0.2)

无需明确指定类型,因为将从您分配的值中推断出该类型.此外,您不需要将文字直接显式构造为特定类型,因为这些文字将与您使用它们的类型一致.

There is no need to specify the type explicitly since it will be inferred from the value you are assigning to. Also, you do not need to explicitly construct your literals as a specific type as those will conform to the type you are using them with.

这篇关于Swift中的分割操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:30