swift 2 Func默认类型参数

时间:2015-11-30 19:45:45

标签: swift generics swift2 swift-protocols

给出以下代码:

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

1 个答案:

答案 0 :(得分:2)

您应该使用Protocol Composition而不使用泛型:

extension NSManagedObject {

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

}