Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

ios - Swift: Conforming to protocols using default values

Is there a way to conform to a Swift protocol using default values?

The following example results in a Type 'FunExample' does not conform to protocol 'Example':

protocol Example {
    func doSomething(with: String)
}

class FunExample: Example {
    func doSomething(with: String, butMakeItFun: Bool = true) {
        // implementation
    }
}

...even though I actually implicitly conform since this is identical to:

protocol Example {
    func doSomething(with: String)
}

class FunExample: Example {
    func doSomething(with: String) {
        self.doSomething(with: with, butMakeItFun: true)
    }
    func doSomething(with: String, butMakeItFun: Bool) {
        // implementation
    }
}

Is there a cleaner way to provide conformance using default values? Or am I missing something conceptually when I say that the first example should also pass conformance?

To clarify: I understand why the error is produced. My question is more conceptual (whether Swift should work this way) and whether there is an elegant way around it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can go with extension for the more cleaner way! Make an extension with the same protocol methods and pass the default values which you want. Let's say:

protocol Example { //Main Protocol
    func doSomething(with: String, butMakeItFun: Bool)
}

extension Example { //Make to pass the default values if you want
    func doSomething(with: String, butMakeItFun: Bool = false) {
        return doSomething(with: with, butMakeItFun: butMakeItFun)
    }
}

class FunExample: Example { //Protocol Acceptance
    func doSomething(with: String, butMakeItFun: Bool) {
        debugPrint("(with)")
        debugPrint(butMakeItFun)
    }
}

You can call it via following ways:

  1. FunExample().doSomething(with: "Hello Default Values...")
  2. FunExample().doSomething(with: "..........", butMakeItFun: true)

Update

So based on your comment, here is what I have came up! You can take a variable under the protocol which surely says true or false as a default implementation and later-on you can change it in your class.

protocol Example {
    var isFun: Bool {set get}
    func doSomething(with: String)
}

class FunExample: Example {

    //var isFun: Bool = false - In case you want to change value

    func doSomething(with: String) {
        doSomething(with: with, butMakeItFun: isFun)
    }

    func doSomething(with: String, butMakeItFun: Bool) {
        debugPrint(with)
        debugPrint(butMakeItFun)
    }
}

extension Example {
    var isFun: Bool {
        get { return true /* Your Default Value */ } set {}
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...