我试图找出答案,但无法解决任何问题。所以,问你们。
是否有机会编写以泛型类型返回的函子?
我具有这样的结构,如下
struct WWSInterest: Unboxable {
var confirmation: Bool
var data: [WWSKeyValue]?
var message: String?
init(unboxer: Unboxer) throws {
self.confirmation = try! unboxer.unbox(key: "confirmation")
self.data = try? unboxer.unbox(key: "data")
self.message = try? unboxer.unbox(key: "message")
}
// static func createFrom(data: Data) -> WWSInterest? {
// do {
// let r: WWSInterest = try unbox(data: data)
// return r
// } catch {
// return nil
// }
// }
}
我想像下面那样帮助func
func create<T>(from data: Data) -> T? {
do {
let r: T = try unbox(data: data)
return r
} catch {
return nil
}
}
有可能吗?
顺便说一句:对不起,我的英语不好
//编辑
WWSKeyValue的结构如下,
struct WWSKeyValue: Unboxable {
var key: String
var value: String
init(unboxer: Unboxer) throws {
self.key = try! unboxer.unbox(key: "key")
self.value = try! unboxer.unbox(key: "value")
}
}