[英]Swift: Conforming to protocols using default values
有沒有辦法使用默認值符合Swift協議?
以下示例導致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
}
}
...盡管我實際上隱含地遵循,因為這等同於:
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
}
}
有沒有一種更干凈的方法來使用默認值提供一致性? 還是當我說第一個示例也應該通過一致性時,我在概念上是否有所遺漏?
需要澄清的是:我理解為什么會產生錯誤。 我的問題是更具概念性的(Swift是否應該以這種方式工作)以及圍繞它是否存在一種優雅的方式。
您可以選擇extension
,以使用更清潔的方式! 使用相同的protocol
方法進行extension
,並傳遞所需的默認值。 比方說:
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)
}
}
您可以通過以下方式調用它:
FunExample().doSomething(with: "Hello Default Values...")
FunExample().doSomething(with: "..........", butMakeItFun: true)
更新資料
因此,根據您的評論,這就是我的想法! 您可以在protocol
下接受一個肯定會說true
或false
的變量作為默認實現,以后您可以在您的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 {}
}
}
在編寫示例代碼時,您的FunExample不符合要求,因為它沒有函數func doSomething(with: String)
。
func doSomething(with: String)
和func doSomething(with: String, butMakeItFun: Bool)
不同。 因此不符合。
當您添加以下代碼時,它將符合
func doSomething(with: String) {
self.doSomething(with: with, butMakeItFun: true)
}
我將提供doSomething(with:String)的默認實現,然后在需要的地方“裝飾”它。
protocol Example {
func doSomething(with: String)
}
extension Example {
func doSomething(with text: String) {
print(text)
}
}
class FunExample: Example {
func doSomething(with: String, butMakeItFun: Bool) {
if butMakeItFun() {
playTheSound()
releaseBalloons()
}
return doSomething(with: String)
}
}
經過研究和思考,事實證明,Swift 不支持這一點在概念上是正確的。
協議一致性的思想是確保您的類滿足某些要求,並且可以毫無問題地提供該功能。 如果存在隱式一致性,則無法實現。
為什么? 考慮一下原始示例,稍作修改:
protocol Example {
func doSomething(with: String)
}
class FunExample: Example {
func doSomething(with: String, butMakeItFun: Bool = true) {
// implementation
}
func doSomething(with: String, andDoSomeOtherStuffWith parameter: String = "") {
// another implementation
}
}
如果 Swift假設由於默認值而使func doSomething(with: String, butMakeItFun: Bool = true)
符合func doSomething(with: String)
,那么這會引入歧義性(因為另一個doSomething函數也符合)。
換句話說,在這里FunExample
實際上並未隱式實現功能doSomething(with: "Asdf")
因為僅在類上調用此函數將導致Ambiguous use of 'doSomething'
錯誤。
因此,從概念上講不可能依靠默認值隱式地假定協議符合性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.