gpt4 book ai didi

ios - Swift 中的分割操作

转载 作者:搜寻专家 更新时间:2023-11-01 06:49:08 25 4
gpt4 key购买 nike

为什么我在这方面经常出错?

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

其实我也试过这个:

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

谢谢

最佳答案

Swift 有 strict rules about the whitespace around operators .除法“/”是一个二元运算符

重要的规则是:

  • If an operator has whitespace around both sides or around neither side, it is treated as a binary operator. As an example, the + operator in a+b and a + b is treated as a binary operator.
  • If an operator has whitespace on the left side only, it is treated as a prefix unary operator. As an example, the ++ operator in a ++b is treated as a prefix unary operator.
  • If an operator has whitespace on the right side only, it is treated as a postfix unary operator. As an example, the ++ operator in a++ b is treated as a postfix unary operator.

也就是说需要在/前加一个空格或者去掉后面的空格来表示它是一个二元运算符:

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

如果你想让 rotation 成为一个 Float,你应该使用它而不是 Double:

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

不需要明确指定类型,因为它会从您分配给的值中推断出来。此外,您无需将文字显式构造为特定类型,因为它们将符合您使用它们的类型。

关于ios - Swift 中的分割操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24964006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com