【问题标题】:Cannot convert the expression's type 'StaticString' to type 'StringLiteralConvertible' (Swift)无法将表达式的类型“StaticString”转换为类型“StringLiteralConvertible”(Swift)
【发布时间】:2014-09-03 14:50:42
【问题描述】:

在 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'

我一辈子都想不通为什么。任何帮助将不胜感激。

【问题讨论】:

    标签: ios xcode generics swift swift-playground


    【解决方案1】:

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

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

    我在您的代码中尝试过,它在操场上运行。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多