swift无法使用类型为((object:Encodable)'的参数列表调用'xxx'

时间:2018-06-25 08:14:53

标签: swift4

当我使用下面的代码调用test2之类的函数时,出现了Cannot invoke 'encodeTest' with an argument list of type '(object: Encodable)'错误。

是代码:

//test func
func encodeTest<T>(object: T) where T: Encodable {
    // The object should be AnyObject which conforms to Encodable.
    let encoder = JSONEncoder()
    if let _ = try? encoder.encode(object) {
        print("encode success.")
        // I need fix the json here.
    }
}

class Test: Encodable {
    var a = "a"
    var b = "b"
}

//test1
var t: Test?
t = Test()
// it is ok.
encodeTest(object: t)


//test2
if let tf = t as Encodable? {
    // Cannot invoke 'encodeTest' with an argument list of type '(object: Encodable)'
    encodeTest(object: tf)
}

调用encodeTest(object: tf)失败,如何解决?感谢您的帮助!

0 个答案:

没有答案