[英]Understanding the Doubles and Ints in Swift
I thought I had a good understanding about Doubles and Ints until I accidentally ran into the following code.我以为我对 Doubles 和 Ints 有很好的理解,直到我不小心遇到了以下代码。
let amounts = [50, 5.0, 10]
var total = 0.0
for i in 0..<amounts.count {
total += amounts[i]
print("Total: \(total)")
}
... but it stops working if I change the 5.0
to 10
or a 50
or even to 5
and generates the following error. ...但是如果我将
5.0
更改为10
或50
甚至更改为5
并生成以下错误,它将停止工作。
error: binary operator '+=' cannot be applied to operands of type 'Double' and 'Int'
错误:二元运算符“+=”不能应用于“Double”和“Int”类型的操作数
Can someone tell me why is that the code doesn't break when mixing 50
, 5.0
and 10
?有人能告诉我为什么混合
50
、 5.0
和10
时代码不会中断吗? I was under the impression that this wouldn't work.我的印象是这行不通。
As you know, Swift is very strict with types, but there's one area where it's not so strict - literals.如您所知,Swift 对类型非常严格,但有一个领域并不那么严格——文字。
Double
conforms to ExpressibleByIntegerLiteral
, so you could do: Double
符合ExpressibleByIntegerLiteral
,所以你可以这样做:
let x: Double = 1 // "1" is "magically" converted to a Double!?
and have it compile.并让它编译。 The same with arrays - the compiler thinks that the array literal that you have:
与 arrays 相同 - 编译器认为您拥有的数组文字:
[50, 5.0, 10]
is a [Double]
, because it can convert both 50
and 10
to Double
.是
[Double]
,因为它可以将50
和10
都转换为Double
。 It can't be an [Int]
because 5.0
can't be converted to an Int
( Int
does not conform to ExpressibleByFloatLiteral
)它不能是
[Int]
因为5.0
不能转换为Int
( Int
不符合ExpressibleByFloatLiteral
)
The line:该行:
total += amounts[i]
only works when both sides are of the same type.仅在双方为同一类型时才有效。 Note that here, the compiler will not try to convert from
Int
to Double
because the expressions involved ( total
and amounts[i]
) are not literals!请注意,在这里,编译器不会尝试从
Int
转换为Double
,因为所涉及的表达式( total
和amounts[i]
)不是文字!
If you change the array literal to [50, 10, 10]
, all elements are Int
, so the compiler infers the array to be [Int]
, and amount[i]
becomes an Int
, causing the line to fail compilation.如果将数组文字更改为
[50, 10, 10]
,则所有元素都是Int
,因此编译器将数组推断为[Int]
,并且amount[i]
变为Int
,导致该行编译失败。
Arrays in Swift can hold elements of a single type, so when you mix 50, 5.0, 10
the compiler will infer the Array
is of type Double
Swift 中的 Arrays 可以容纳单一类型的元素,因此当您混合 50、5.0、10 时
50, 5.0, 10
编译器将推断Array
是Double
类型
In the working code, the array is an array of [Doubles]
when you change 5.0 to 10 the array is of [Int]
that because of swift Type Inference.在工作代码中,当您将 5.0 更改为 10 时,数组是
[Doubles]
的数组,由于 swift 类型推断,数组是[Int]
的数组。
Thus因此
Binary operator '+=' cannot be applied to operands of type 'Double' and 'Int'二元运算符“+=”不能应用于“Double”和“Int”类型的操作数
Swift Compiler don't know operation about two different data-type like Int and Double. Swift 编译器不知道有关 Int 和 Double 等两种不同数据类型的操作。
So you can achieve this by type cast from Int to Double using this Code.因此,您可以使用此代码通过从 Int 到 Double 的类型转换来实现这一点。
let amounts = [50, 5, 10]
var total = 0.0
for i in 0..<amounts.count {
total = total + Double(amounts[i])
print("Total: \(total)")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.