谁能解释一下为什么func3和func4不符合协议吗?

问题描述 投票:0回答:1

这不能在 Swift 5.10 中编译。

protocol Common {
  func func1() -> Common
  func func2() -> Common
  func func3() -> Common
  func func4() -> Common
}

struct Model: Common {
  func func1() -> any Common {self}
  func func2() -> Common     {self}
  func func3() -> Self       {self}    // Not conformant
  func func4() -> Model      {self}    // Not conformant   
}

我不清楚为什么 Self 和 Model 不符合 Common 协议。我也不会想到添加“any”,但这就是 Xcode 的“fix”生成的。

swift protocols
1个回答
0
投票

协议“准确地”描述了采用者必须说的话。 func func3() -> Common 表示采用者必须按字面意思说

func func3() -> Common
才能保持一致。
    

© www.soinside.com 2019 - 2024. All rights reserved.