到目前为止,我正在使用type(of: )函数来查找变量的动态类型,并将其与Type.self进行比较以检查该类型:
var x = 5
if(type(of: x) == Int.self)
{
print("\(x) is of type Int")
}我做得对吗?或者是否有更好/更好的方法来检查类型?
发布于 2022-05-09 11:41:40
我个人会用
if(x is Int)
{
print("\(x) is of type Int")
}如果您期望x是一个整数,而不是使用typeof,因为它更易读。但当然,如果您愿意,可以使用typeOf。两者都是对的
发布于 2022-05-09 10:16:05
你可以使用;
if (x is Int) {
print("\(x) is of type Int")
}https://stackoverflow.com/questions/72170268
复制相似问题