Make a protocol conform to another protocol

前端 未结 4 1359
臣服心动
臣服心动 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:35

    Here is how you require conformance to protocols in extensions.

    extension Pen where Self: InstrumentForProfessional {}
    

    The current way that you are doing makes the compiler think that you are doing inheritance, not protocol conformance.

    Also note that let pen = ApplePen() as InstrumentForProfessional doesn't make sense and won't compile.

提交回复
热议问题