Swift 将 Int 与 Int32 或 Int64 进行比较

标签 swift

我注意到如果我在 int 和 int64 上使用 == 登录,似乎没有警告和错误。那么这是否意味着我可以安全地使用 == 来与 int 和 int32/64 进行比较?

我在 Playground 上试过了,结果是正确的。

enter image description here

在问这个问题之前尝试搜索,注意到大多数答案都是从 Int 转换为 Int64。但是没有人质疑我们是否可以使用 == 来比较 IntInt64

最佳答案

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 the Int, UInt, and UInt8 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."

来自 BinaryInteger - Comparing Across Integer Types

您可以阅读 the docs for this particularity overload of == ,也是。

关于Swift 将 Int 与 Int32 或 Int64 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49885785/

相关文章:

ios - 带有模型的 SwiftyJson 嵌套数组

ios - 如何在 iOS 上使用 Sign In with Google 修复 `The user canceled the sign-in flow.`?

ios - 我无法导入我需要的东西,以使用 MaterialContainerScheme

swift - 在 Swift 中循环创建字典

swift - 了解数组和运算符的使用

ios - 应用程序更新时崩溃

swift - Xcode 9 自定义模板如何改变?

Swift 3 可选展开

swift - 是否可以在 Controller 中定期执行测试?

swift 包管理器。错误 : Found multiple packages with the name. ..?