加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 打开Any.Type

发布时间:2020-12-14 04:46:13 所属栏目:百科 来源:网络整理
导读:我正试图在 swift中打开一个类型.我不是试图打开对象实例的类型,我试图打开实际类型本身.例如: let t: Any.Type = Int.self switch t { case is Int: print("int") default: print("other") } 我希望这打
我正试图在 swift中打开一个类型.我不是试图打开对象实例的类型,我试图打开实际类型本身.例如:

let t: Any.Type = Int.self
 switch t {
 case is Int:
     print("int")
 default:
     print("other")
 }

我希望这打印“int”但它属于默认情况.

我可以使用if语句完成所需的结果,如,

if t == Int.self
 {
     print("t is an int")
 }

但是我希望通过开关来实现这一目标.我读过Apple的’Type Casting’文档,可能还不够彻底,因为我看不到在这里应用它的方法.

解决方法

Xcode在上述情况下生成以下警告:“从’Any.Type’转换为无关的Type’Int’总是失败”,这提示了正确的方法:

let t: Any.Type = Int.self
 switch t {
 case is Int.Type:
     print("Int")
 default:
     print("Other")
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读