多协议和类继承混淆,Swift

huangapple go评论62阅读模式
英文:

Multiple protocol and class inheritance confusion, Swift

问题

'PaymentViewModelConfigurable' 需要 'PaymentViewModel' 继承自 'ViewModelWithResult'。

类型 'PaymentViewModel' 不符合协议 'PaymentViewModelConfigurable'。

英文:

So I know a class can only inherit a single class but can inherit multiple protocols. I am looking at some code here and confused as to why swift is throwing me an error.

protocol PaymentViewModelConfigurable: ViewModelWithResult {
    
}

class ViewModelWithResult {
    func printWithResultClass() {
        print("In View Model with result class")
    }
}

class PaymentViewModel: PaymentViewModelConfigurable {
    
    
}


class MainOne {
    let viewModel: PaymentViewModelConfigurable = PaymentViewModel()
}

So I would assume this is ok because my PaymentViewModel class inherits a protocol and that protocol inherits from a class.

But if I change the logic to this on my ViewModel to inherit the protocol & the class, its fine 😕

protocol PaymentViewModelConfigurable: ViewModelWithResult {
    func payments()
}

class ViewModelWithResult {
    func printWithResultClass() {
        print("In View Model with result class")
    }
}

class PaymentViewModel: ViewModelWithResult, PaymentViewModelConfigurable {
    func payments() {
        print("Payments")
    }
}


class MainOne {
    let viewModel: PaymentViewModelConfigurable = PaymentViewModel()
    
    init() {
        viewModel.payments()
    }
}

These are the errors that come up:

>'PaymentViewModelConfigurable' requires that 'PaymentViewModel' inherit from 'ViewModelWithResult'

>Type 'PaymentViewModel' does not conform to protocol 'PaymentViewModelConfigurable'

答案1

得分: 2

我知道一个类只能继承一个类,但可以遵循多个协议。

不完全正确。一个类可以遵循多个协议。这可能看起来是对措辞的一种追求,但实际上这与问题的核心相关。遵循和继承特性是相关联的,但又不同。类似地:

并且该协议继承自一个类。

它并不继承。

在协议声明的冒号后面的一切都不是超类和协议遵循的列表,就像类一样。它是该协议的要求列表。所以这个声明:

protocol PaymentViewModelConfigurable: ViewModelWithResult {
    func payments()
}

是在说:“PaymentViewModelConfigurable是一个协议,可以被任何ViewModelWithResult的子类型遵循,并具有func payments()方法。这就好像你写了:

protocol PaymentViewModelConfigurable where Self: ViewModelWithResult {
    func payments()
}

错误消息非常明确地说明了这一点:

Untitled 4.swift:11:7: error: 'PaymentViewModelConfigurable' requires that 'PaymentViewModel' inherit from 'ViewModelWithResult'
class PaymentViewModel: PaymentViewModelConfigurable {
      ^
Untitled 4.swift:11:7: note: requirement specified as 'Self' : 'ViewModelWithResult' [with Self = PaymentViewModel]
class PaymentViewModel: PaymentViewModelConfigurable {
      ^
英文:

> So I know a class can only inherit a single class but can inherit multiple protocols.

Not quite. A class can conform to multiple protocols. It might seem like a pedantic correction of wording, but it's actually core to the issue here. The conformance and inheritance features are related and interconnected, but distinct. Similarly:

> and that protocol inherits from a class.

It doesn't.

Everything after the : on a declaration of a protocol is not a list of superclasses and protocol conformances, like with classes. It's a list of requirements of that protocol. So this declaration:

protocol PaymentViewModelConfigurable: ViewModelWithResult {
    func payments()
}

Is saying: "PaymentViewModelConfigurable is a protocol that can be conformed to anything that's a subtype of ViewModelWithResult and has a func payments() method. It's as if you had written:

protocol PaymentViewModelConfigurable where Self: ViewModelWithResult {
    func payments()
}

The error messages spell this out quite explicitly:

Untitled 4.swift:11:7: error: 'PaymentViewModelConfigurable' requires that 'PaymentViewModel' inherit from 'ViewModelWithResult'
class PaymentViewModel: PaymentViewModelConfigurable {
      ^
Untitled 4.swift:11:7: note: requirement specified as 'Self' : 'ViewModelWithResult' [with Self = PaymentViewModel]
class PaymentViewModel: PaymentViewModelConfigurable {
      ^

huangapple
  • 本文由 发表于 2023年7月7日 03:22:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定