복붙노트

[SWIFT] 스위프트 : 유형이 지정된 클래스의 서브 클래스를 프로토콜을 구현해야합니다 [중복]

SWIFT

스위프트 : 유형이 지정된 클래스의 서브 클래스를 프로토콜을 구현해야합니다 [중복]

해결법


  1. 1.당신은 이런 식으로 작업을 수행 할 수 있습니다 :

    당신은 이런 식으로 작업을 수행 할 수 있습니다 :

    protocol SomeProtocol {
      func someMethodInSomeProtocol()
    }
    
    class SomeType { }
    
    class SomeOtherType: SomeType, SomeProtocol {
      func someMethodInSomeProtocol() { }
    }
    
    class SomeOtherOtherType: SomeType, SomeProtocol {
      func someMethodInSomeProtocol() { }
    }
    
    func someMethod<T: SomeType where T: SomeProtocol>(condition: Bool) -> T {
      var someVar : T
      if (condition) {
        someVar = SomeOtherType() as T
      }
      else {
        someVar = SomeOtherOtherType() as T
      }
    
      someVar.someMethodInSomeProtocol()
      return someVar as T
    }
    

    이 함수를 정의하는 반환 형식 'SomeType'프로토콜 'SomeProtocol'반환의 객체를 객체가 이러한 조건을 준수합니다.

  2. from https://stackoverflow.com/questions/25825988/swift-type-must-implement-protocol-and-be-a-subclass-of-given-class by cc-by-sa and MIT license