2014-09-03 3 views
4

iOS에서 실행되는 프로그래밍 언어의 제네릭은 무엇입니까? 닥치고 내 돈을 가져가!표현식 유형 'StaticString'을 'StringLiteralConvertible'(Swift) 유형으로 변환 할 수 없습니다.

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

I :

var foo = Foo.floop("test") 

마지막 부분에 오류 메시지가 발생합니다 :에 의해

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) 
    } 
} 

이어 :

여기에 내 실험 코드 (이 놀이터에서 실행)입니다 왜 내 인생에서 그럴 수 없습니까? 어떤 도움이라도 대단히 감사 할 것입니다.

답변

1

비슷한 문제가 발견되어 클래스를 인스턴스화하는 순간에 유형을 지정하여 작업을 수행 할 수 있습니다. 즉, 클래스가 인스턴스화 된 후 제네릭이 어떤 유형인지 알려야합니다. 이렇게 :

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

나는 당신의 코드에서 그것을 시도하고 그것은 놀이터에서 일했다.

희망이 도움이됩니다.