我注意到如果我在 int 和 int64 上使用 ==
登录,似乎没有警告和错误。那么这是否意味着我可以安全地使用 ==
来与 int 和 int32/64 进行比较?
我在 Playground 上试过了,结果是正确的。
在问这个问题之前尝试搜索,注意到大多数答案都是从 Int 转换为 Int64。但是没有人质疑我们是否可以使用 ==
来比较 Int
和 Int64
最佳答案
Comparing Across Integer Types
You can use relational operators, such as the less-than and equal-to operators (
<
and==
), to compare instances of different binary integer types. The following example compares instances of theInt
,UInt
, andUInt8
types:let x: Int = -23 let y: UInt = 1_000 let z: UInt8 = 23 if x < y { print("\(x) is less than \(y).") } // Prints "-23 is less than 1000." if z > x { print("\(z) is greater than \(x).") } // Prints "23 is greater than -23."
关于Swift 将 Int 与 Int32 或 Int64 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49885785/