Make a protocol conform to another protocol

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

    Two answers have already been provided: @user28434 is providing you with a solution under the assumption that you can add the conformance at the time that you write the Pen protocol and @paper1111 is providing you with the opportunity to make additions to the Pen extension exclusive to where the type also conforms to InstrumentForProfessional. Note: to take advantage of @paper1111's answer you must also add the protocol to your type like so:

    class ApplePen: Pen, InstrumentForProfessional {
      var title: String = "CodePen"
      var color: UIColor = .blue
    }
    

    Which seems to stray further from your requirements than the answer from @user28434, and in fact is answering a different question (which is how to add functionality to a type that adopts two different protocols). Therefore I would ask whether what you are actually looking for is not protocol but class inheritance:

    class InstrumentForProfessional {
        var title: String
        init(title:String) {
            self.title = title
        }
    }
    
    class Pen: InstrumentForProfessional {
        var color: UIColor
        init(title:String, color:UIColor) {
            self.color = color
            super.init(title: title)
        }
    }
    

    Because it seems that what you are getting at through the existence of the title property in both is the overriding behaviour common to class inheritance. So the question becomes why wrestle to squeeze class inheritance into a protocol when you are using class rather than struct or enum anyway?

    If you don't want to apply class inheritance and you don't want to add inheritance at the time of writing the Pen protocol, and if you also don't want to add multiple protocols to your class, then one other thing you could do for neatness is to use a typealias:

    protocol InstrumentForProfessional {
        var title: String {get}
    }
    
    protocol PenExtra {
        var color: UIColor {get}
        var title: String {get}
    }
    
    typealias Pen = InstrumentForProfessional & PenExtra
    
    class ApplePen: Pen {
        var title = "CodePen"
        var color = UIColor.blue
    }
    

    But having written all this, if you can follow @user28434's approach then do so.

提交回复
热议问题