Make a protocol conform to another protocol

前端 未结 4 1362
臣服心动
臣服心动 2021-02-12 11:57

I have two protocols: Pen and InstrumentForProfessional. I\'d like to make any Pen to be an InstrumentForProfessional:

protoc         


        
4条回答
  •  遥遥无期
    2021-02-12 12:39

    I think if you check over at this SO answer, it solves the same problem.

    https://stackoverflow.com/a/37353146/1070718

    @paper1111 was close to what you're looking for, but I think you really want to do :

    extension InstrumentForProfessional where Self: Pen {}
    

    Since Pen already conforms to InstrumentForProfessional then you just need to extend InstrumentForProfessional when it is a Pen.

    Sometimes I forget how protocol inheritance works in Swift but thanks to SO for refreshing my memory.

提交回复
热议问题