swift4 - swift 无法使用类型为“(对象:可编码)”的参数列表调用“xxx”
问题描述
Cannot invoke 'encodeTest' with an argument list of type '(object: Encodable)'
当我使用下面的代码调用 test2 之类的函数时,出现错误。
这是代码:
//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)
,如何解决?谢谢你的帮助!
解决方案
推荐阅读
- angular - 无法使用选择器和 AsyncPipe 显示 NgRx 存储中的值
- python - PySide - 网格的保持比例
- android - 有没有一种方法可以为每个视图使用多个变量(不同的数据类型)在相同的布局中实现数据绑定?
- angular - 将其设置为 mat 表数据源时未定义的分页器
- android - 检测 Android 应用程序恢复和暂停状态
- android - 有没有办法将drawable转换为ImageVector?
- node.js - Express Req.body 在一台 PC 上为空,但在另一台 PC 上工作正常
- python - 当从电话应用程序中提及时,discord.py bot 没有回答
- postgresql - 如何在 Airflow 中将 Postgres 设置为后端数据库?
- python - 文件处理中的for循环(python)