无法将表达式的类型“StaticString”转换为类型“StringLiteralConvertible”(Swift)

4

在iOS上运行的编程语言中有泛型?闭嘴并拿走我的钱!

这是我的实验性代码(在playground中运行):

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'

我真的想不出来为什么。非常感谢您提供的任何帮助。

1个回答

1
我发现了一个类似的问题,我可以通过在实例化类时指定类型来解决它,也就是说,你必须告诉泛型在类被实例化后将拥有什么类型,在你的情况下,可能会像这样:
var foo = Foo<String>.floop("test")  //or,
var foo = Foo<Int>.floop("test")

我在你的代码中尝试了一下,在游乐场里可以运行。

希望这有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接