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

swift – 将类型传递给泛型函数并进行比较

发布时间:2020-12-14 04:41:06 所属栏目:百科 来源:网络整理
导读:考虑这个简单的例子 func containsT(type t: T.Type) - Bool { for i in 0..list.count { if list[i] is t { // compiler says: 't' is not a type return true } } return false} 编译器响应 't' is not a type 由于我无法声明静态类型并使用MyStaticType进
考虑这个简单的例子

func contains<T>(type t: T.Type) -> Bool {
    for i in 0..<list.count {
        if list[i] is t { // compiler says: 't' is not a type
            return true
        }
    }
    return false
}

编译器响应

't' is not a type

由于我无法声明静态类型并使用MyStaticType进行检查,我如何使用泛型在Swift中完成此操作?

解决方法

你应该检查它是否是T:

if list[i] is T {
    ...
}

(编辑:李大同)

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

    推荐文章
      热点阅读