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

ios – 无法将表达式的类型’StaticString’转换为’StringLiter

发布时间:2020-12-14 17:23:17 所属栏目:百科 来源:网络整理
导读:在iOS上运行的编程语言中的泛型?不必多说了,把我的钱拿去! 这是我的实验代码(这是在游乐场中运行): class FooGenericType1 { var value:GenericType1? init(value:GenericType1) { self.value = value; }}class Foo2GenericType2 : FooGenericType2 { ove
在iOS上运行的编程语言中的泛型?不必多说了,把我的钱拿去!

这是我的实验代码(这是在游乐场中运行):

class Foo<GenericType1> {
    var value:GenericType1?
    init(value:GenericType1) {
        self.value = value;
    }
}

class Foo2<GenericType2> : Foo<GenericType2> {
    override init(value:GenericType2) {
        super.init(value:value);
    }
}

extension Foo {
    class func floop<T>(value: T) -> Foo2<T> {
        return Foo2<T>(value:value)
    }
}

其次是:

var foo = Foo.floop("test")

最后一部分抛出一条错误消息:

Cannot convert the expression's type 'StaticString' to type 'StringLiteralConvertible'

我不能为我的生活找出原因.任何帮助将非常感激.

解决方法

我发现了一个类似的问题,我可以通过在实例化类的时刻指定类型来使它工作,即你必须告诉一旦类实例化后泛型将具有什么类型,在你的情况下它可能是这样的:

var foo = Foo<String>.floop("test")  //or,var foo = Foo<Int>.floop("test")

我在你的代码中尝试了它,它在游乐场工作.

希望这可以帮助!

(编辑:李大同)

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

    推荐文章
      热点阅读