Reputation: 1279
I declare two variables in a Swift file, with the following values:
let m = 1
let n = 2
Now, I would like to compare their types:
type(of: m) === type(of: n)
The following compliling error occurs:
Argument type 'String.Type' expected to be an instance of a class or class-constrained type
Is there a way to compare types ?
Also, why does this line fail ?
String === String
Upvotes: 1
Views: 1278
Reputation: 54805
The ===
operator is only defined for reference types, since it checks reference (pointer) equality, so you can't use it on metatypes of value types.
However, you can use the normal equality operator, ==
.
type(of: m) == type(of: n)
As for String === String
, that's not even valid code. What you need if String.self == String.self
. You can access metatypes using TypeName.self
.
Upvotes: 4