저는 Xcode 7.3과 Swift 2.3을 사용하고 있습니다. 변수가있는 관련 유형의 프로토콜을 사용하는 데 어려움이 있습니다. 예 봐 : 어떤 이유관련 유형이있는 프로토콜의 일반 변수입니다. 곤충?
protocol SomeProtocol {}
class SomeProtocolImpl: SomeProtocol {}
protocol ProtocolWithAssociatedType {
associatedtype T: SomeProtocol
var variable: T { get }
}
class TestClass: ProtocolWithAssociatedType {
var variable: SomeProtocol = SomeProtocolImpl()
}
, 컴파일러는 오류를 보여줍니다 어떻게 가능? 내가 뭔가 잘못하고 있는거야? 그게 버그 야? 알려진 것? 나는 무엇을 시도했다
: 그 연결 유형에 대한
정의 typealias :
class TestClass: ProtocolWithAssociatedType {
typealias O = SomeProtocol
var variable: SomeProtocol = SomeProtocolImpl()
}
싫다. 대신
이 오류를 방지하는 방법은 관련 유형과 변수와 프로토콜을 만들어야합니다
protocol SomeProtocol {}
class SomeProtocolImpl: SomeProtocol {}
protocol ProtocolWithAssociatedType {
associatedtype T: SomeProtocol
func someMethod() -> T
}
class TestClass: ProtocolWithAssociatedType {
typealias T = SomeProtocol
func someMethod() -> SomeProtocol {
return SomeProtocolImpl()
}
}
그냥 다른 오류가있어?
는 @Hamish 그래, 참으로, 당신은 바로 –