gpt4 book ai didi

具有关联类型的快速协议(protocol)

转载 作者:行者123 更新时间:2023-12-04 03:55:23 25 4
gpt4 key购买 nike

如何纠正错误?!

领域模块,包含所有摘要

//MARK: - Domain
protocol TypeB {
associatedtype P
func makeP() -> P
init()
}

protocol TypeA {
associatedtype P
associatedtype B: TypeB

func makeP() -> P
init(objB: B)
}

protocol FirstTargetFabricType {
func makeB<B: TypeB>() -> B where B.P == Int
}

protocol SecondTargetFabricType {
func makeA<B, A: TypeA>(objcB: B) -> A where A.B == B, A.P == B.P, B.P == Int
}

第一个模块,例如与网络一起工作的模块

//MARK: - First Target
class ClassB: TypeB {
required init() { }
func makeP() -> Double { return 10 }
}


// error: Cannot convert return expression of type 'ClassB' to return type 'B'
class FirstTargetFabric: FirstTargetFabricType {
func makeB<B: TypeB>() -> B where B.P == Int {
return ClassB()
}
}

第二个模块,例如处理存储的模块

//MARK: - SecondTarget
class ClassA<B: TypeB>: TypeA {
let objB: B

required init(objB: B) {
self.objB = objB
}

func makeP() -> B.P { return objB.makeP() }
}

class SecondTargetFabric: SecondTargetFabricType {
func makeA<B, A: TypeA>(objcB: B) -> A where B == A.B, B.P == A.P, B.P == Int {
return A(objB: objcB)
}
}

应用目标

//MARK: - app
let firstFabric: FirstTargetFabricType = FirstTargetFabric()
let secondFabric: SecondTargetFabricType = SecondTargetFabric()

/// error: Generic parameter 'A' could not be inferred
let res = secondFabric.makeA(objcB: firstFabric.makeB())

我正在尝试制作一个用于分页的服务加载器。我只想在类初始值设定项中传递一个将进入网络的实体

最佳答案

这种方法有多好?

 //MARK: - Domain
open class TypeB<T: Decodable> {
open func makeP() -> T {
fatalError("Required function have not been implemented")
}
}

open class TypeA<T: Decodable> {
open func makeP() -> T {
fatalError("Required function have not been implemented")
}
}

public protocol FirstTargetFabricType {
func makeInt() -> TypeB<Int>
func makeDouble() -> TypeB<Double>
}

public protocol SecondTargetFabricType {
func makeInt(objInt: TypeB<Int>) -> TypeA<Int>
func makeDouble(objDouble: TypeB<Double>) -> TypeA<Double>
}



//MARK: - First Target
internal class ClassB: TypeB<Int> {
override func makeP() -> Int { return 10 }
}

internal class ClassBB: TypeB<Double> {
override func makeP() -> Double { return 20 }
}

public class FirstTargetFabric: FirstTargetFabricType {
public func makeInt() -> TypeB<Int> { ClassB() }
public func makeDouble() -> TypeB<Double> { ClassBB() }
}



//MARK: - SecondTarget
internal class ClassA: TypeA<Int> {
let objB: TypeB<Int>

init(objB: TypeB<Int>) {
self.objB = objB
}

override func makeP() -> Int {
objB.makeP()
}
}

internal class ClassAA: TypeA<Double> {
let objB: TypeB<Double>

init(objB: TypeB<Double>) {
self.objB = objB
}

override func makeP() -> Double {
objB.makeP()
}
}

public class SecondTargetFabric: SecondTargetFabricType {
public func makeInt(objInt: TypeB<Int>) -> TypeA<Int> { ClassA(objB: objInt) }
public func makeDouble(objDouble: TypeB<Double>) -> TypeA<Double> { ClassAA(objB: objDouble) }
}


//MARK: - app
let first = FirstTargetFabric()
let second = SecondTargetFabric()

let objInt = first.makeInt()
let objDouble = first.makeDouble()


print(second.makeInt(objInt: objInt).makeP())
print(second.makeDouble(objDouble: objDouble).makeP())

关于具有关联类型的快速协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63987596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com