ios - 将类扩展继承子句迁移到协议(protocol)扩展
<p><p>我有一个 <code>VehicleModels</code> 框架,其中包含 <code>Car</code>、<code>Bike</code>、<code>Plane</code> 等类。
在另一个框架 <code>VehicleInventory</code> 中,我需要在表格中打印自定义描述(特定于第二个框架)。所以我添加了协议(protocol) <code>DescriptableVehicle</code> 和方法 <code>describe()</code>。
然后我为所有车辆添加了协议(protocol)扩展,例如:</p>
<pre><code>extension Car: DescriptableVehicle {
func describe() -> String {
return "Car: \(self.vin)" // returns formatted vehicle number
}
}
</code></pre>
<p>但是,假设已经改变,现在我不会从我的车辆框架中公开具体的类。相反,我公开了诸如 <code>CarProtocol</code>、<code>BikeProtocol</code> 之类的协议(protocol),因此通常我拥有相同的信息。 </p>
<p>问题是我不能再使用协议(protocol)扩展(或者至少不能以那种形式),因为与 <strong>类扩展</strong>相反的<strong>协议(protocol)扩展</strong>不能有继承子句。</p>
<p>知道如何解决这个问题而不是过多地修改我的用法吗?
最初,我认为协议(protocol)上的几个 <code>where</code> 子句加上几个强制转换会达成交易,但是如果没有访问类,它就没有帮助。
我也尝试过适配器和类型删除,但要么我使用不当,要么它用于不同的目的。</p>
<p>为了说明问题,我准备了存储库:<a href="https://github.com/wedkarz/SwiftProtocolExtensionsProblem" rel="noreferrer noopener nofollow">https://github.com/wedkarz/SwiftProtocolExtensionsProblem</a> </p>
<p>有两个 Playground 。 <strong>V1</strong> 是我曾经拥有并且正在工作的。
<strong>V2</strong> 包含我现在拥有的以及我正在努力实现的功能。</p>
<p>在现实生活中,<code>PrivateFramework</code> 类是一个单独的框架和协议(protocol):<code>VehicleProtocol</code>、<code>CarProtocol</code>、<code>BikeProtocol</code>、 <code>PlaneProtocol</code> 是其中的一部分,但 <code>DescriptableVehicle</code> <strong>不是</strong> <code>PrivateFramework</code> 的一部分,所以不能在里面使用。该示例说明了访问具体类型的问题及其扩展的问题。</p>
<p>你可以看到有一些扩展被注释掉了,因为我不能再使用它们了。目标是使 <code></code> 的集合以与以前类似的方式打印其内容。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这能回答你的问题吗?</p>
<pre><code>class PrivateFramework {
private class AbstractVehicle: VehicleProtocol {
var name: String { return "Abstract vehicle name" }
}
private class Car: AbstractVehicle, CarProtocol {
override var name: String { return "Car vehicle name" }
let vin: String = "ABCDEFGH VIN"
}
private class Bike: AbstractVehicle, BikeProtocol {
override var name: String { return "Bike vehicle name" }
let saddle: String = "Comforable saddle name"
}
private class Plane: AbstractVehicle, PlaneProtocol {
override var name: String { return "Plane vehicle name" }
let numberOfEngines: Int = 4
}
func produceVehicles() -> {
let car = Car()
let bike = Bike()
let plane = Plane()
return
}
}
protocol VehicleProtocol {
var name: String { get }
}
protocol DescriptableVehicle: VehicleProtocol {
func describe() -> String
}
protocol CarProtocol: DescriptableVehicle {
var vin: String { get }
}
protocol BikeProtocol: DescriptableVehicle {
var saddle: String { get }
}
protocol PlaneProtocol: DescriptableVehicle {
var numberOfEngines: Int { get }
}
extension DescriptableVehicle where Self: CarProtocol {
func describe() -> String { return "Car, \(self.name), \(self.vin)" }
}
extension DescriptableVehicle where Self: BikeProtocol {
func describe() -> String { return "Bike, \(self.name), \(self.saddle)" }
}
extension DescriptableVehicle where Self: PlaneProtocol {
func describe() -> String { return "Plane, \(self.name), \(self.numberOfEngines)" }
}
let vehicles: = PrivateFramework().produceVehicles()
vehicles.forEach { print($0.describe()) }
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将类扩展继承子句迁移到协议(protocol)扩展,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47436106/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47436106/
</a>
</p>
页:
[1]