0

我试图找出但无法解决任何问题。所以,请教各位。

有没有机会编写一个作为泛型返回的函数?

我的结构如下

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 create<T>(from data: Data) -> T? {
    do {
        let r: T = try unbox(data: data)
        return r
    } catch {
        return nil
    }
}

那可能吗?

BTW:对不起我的英语不好

// 编辑

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")
    }
}
4

0 回答 0