nuynait
nuynait

Reputation: 1962

Swift compare Int with Int32 or Int64

I noticed if I use == sign on int and int64, there seems no warning and error on that. So does that mean I can use == safely to compare with int and int32/64?

I tried this in playground and the result is correct.

enter image description here

Tried search before asking this question, noticed that most answers are casting from Int to Int64. But no one questioning about if we can use == to compare Int and Int64

Upvotes: 1

Views: 1796

Answers (1)

Alexander
Alexander

Reputation: 63262

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."

From BinaryInteger - Comparing Across Integer Types

You can read the docs for this particularity overload of ==, too.

Upvotes: 1

Related Questions