Swift:为什么协议不能被继承

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

这是我的代码:

protocol base{}

protocol sub: base{}

protocol SomeProtocol{
    var a: base{get}
}

struct SomeStruct: SomeProtocol{
//    var a: base
    var a: sub
}

当我在 Struct 中使用

var a: sub
时,出现错误:

Type 'SomeStruct' does not conform to protocol 'SomeProtocol'

我想知道

var a: sub
应该符合基本协议,但为什么Swift不能这样写?有什么更好的方案吗?

谢谢!

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