ios – 无法将表达式的类型’StaticString’转换为’StringLiteralConvertible’类型(Swift)

前端之家收集整理的这篇文章主要介绍了ios – 无法将表达式的类型’StaticString’转换为’StringLiteralConvertible’类型(Swift)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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")

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

希望这可以帮助!

猜你在找的Xcode相关文章