gpt4 book ai didi

Swift:func返回类型继承类并符合一个或多个协议(protocol)

转载 作者:行者123 更新时间:2023-11-28 12:27:34 24 4
gpt4 key购买 nike

从外部库中,我得到了以下场景。

协议(protocol)和基类:

protocol P1 {
// P1 Stuff
}

class A {
// A Stuff
}

然后有一个扩展,导致我的麻烦。它是为 P1A 类型的组合定义的。

extension P1 where Self : A {
public func myDesiredFunc() {
// Do stuff.
}
}

最后是我使用的实现 B1 - Bn

class B1 : A, P1 {
}
class B2 : A, P1 {
}
...

在我的代码中,我需要将 B 类的实例放在一起并使用它们。问题是,我需要使用扩展函数 myDesiredFunc()。所以我需要以某种方式定义,数组类型类似于 [(A & P1)] 并且生成函数的返回类型也是 (A & P1)

但是,使用这段代码:

func createBeeInstance() -> (A & P1) {
if something {
return B1()
} else if something {
return B2()
}
...
}

我收到错误:

Non-protocol type 'A' cannot be used within a protocol composition.

有没有办法说返回类型是类和协议(protocol)的组合?我无法更改方案,因为它是一个外部库。

最佳答案

swift 4:

^_^
现在可以在 swift 4 中组合类和协议(protocol),因此修改之前的代码片段应该可以工作

class A {
// A Stuff
required init () {}
}
protocol P1 {
// P1 Stuff
}

class B1 : A {}
class B3: P1 {}
class B2 : A, P1 {}

func createBeeInstance<T: P1 & A>(type: T.Type) -> T {
return type.init()
}

var things = [P1 & A]() // still Element has to be P1 as well
let f = createBeeInstance(type: B2.self)
//let f1 = createBeeInstance(type: B1.self) // will error
//let f2 = createBeeInstance(type: B3.self) // will error

things.append(f) // is fine

--- 旧方法无效 ---

你可以这样使用,让您提供的样本修改类 A 以具有 init

class A {
// A Stuff
required init () {}
}

修改createBee方法为

func createBeeInstance<T: P1>(type: T.Type) -> T where T: A {
return type.init()
}

通过这种方式,您将提供类型作为输入,例如B1.self

对于数组我们可以提供typealiased泛型

typealias B<T:P1> = T where T: A

var things = [B<A>]() // still Element has to be P1 as well
let f = createBeeInstance(type: B2.self)
let f1 = createBeeInstance(type: B1.self)
things.append(f)

关于Swift:func返回类型继承类并符合一个或多个协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42924481/

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