swift 2 Func 默认类型参数
swift 2 Func Default Type Argument
给定以下代码:
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 函数上给我一个错误,说
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
}
}
给定以下代码:
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 函数上给我一个错误,说
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
}
}