在Swift中找到“ struct”类型的变量类型的正确方法是什么?
截至目前,我正在使用type(of:)
函数来找出变量的动态类型,并将其与type.selp.selp
进行比较以检查类型:
var x = 5
if(type(of: x) == Int.self)
{
print("\(x) is of type Int")
}
am我做对吗?还是有什么更好/优先检查类型的方法?
As of now, I am using type(of: )
function to find out the dynamic type of a variable and I compare it with Type.self
to check the type :
var x = 5
if(type(of: x) == Int.self)
{
print("\(x) is of type Int")
}
Am I doing it right ? Or is there any better/preferred way to check the type ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望X是整数,那么我个人会使用
而不是使用TypeOf,因为它更可读。但是当然,您可以使用TypeOf。两者都是同样正确的
i personaly would use
rather than using typeof if you're expecting x to be an integer as it's far more readable. but sure, you can use typeOf if you want to. Both are equally right
您可以使用;
You can use;