gpt4 book ai didi

swift 2 Func 默认类型参数

转载 作者:行者123 更新时间:2023-11-28 13:00:33 26 4
gpt4 key购买 nike

给定以下代码:

protocol NetworkWire {
//some requirements
}

protocol EntityRESTRequest {
//some requirements
}

protocol OctupPromisable {
//some requirements
}

final class HTTPNetworkWire: NetworkWire, EntityRESTRequest, OctupPromisable {
//satisfies all requirements
}

我现在像这样创建一个函数,

extension NSManagedObject {

func post<T where T:NetworkWire, T:EntityRESTRequest, T:OctupPromisable>(navigationalProperties: String, networkWireType: T.Type = HTTPNetworkWire) -> OctupPromisable {
//some logic with valid return
}

}

编译器在 post func 上给我一个错误,说,

HTTPNetworkWire.Type 的默认参数无法转换为类型 T.type

知道这是为什么吗?尽管 HTTPNetworkWire 符合 NetworkWire,EntityRESTRequest 以及 OctupPromisable!

任何想法将不胜感激。运行 Xcode 7.1.1

最佳答案

你应该使用 Protocol Composition没有泛型:

extension NSManagedObject {

func post(navigationalProperties: String, networkWireType: protocol<NetworkWire, EntityRESTRequest, OctupPromisable>.Type = HTTPNetworkWire.self) -> OctupPromisable {
//some logic with valid return
}

}

关于swift 2 Func 默认类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34006691/

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