我有一个协议,即 Codable 和一个类,即 Codable :
public protocol SourceListItem: AnyObject, Codable
{
var name: String { get set }
var children: [SourceListItem] { get set }
}
final public class SourceListHeader: Codable
{
var name: String = "Give me a name!"
var children: [SourceListItem] = [SourceListItem]()
}
但是,编译器给了我两个错误:
Type 'SourceListHeader' does not conform to protocol 'Decodable'
Type 'SourceListHeader' does not conform to protocol 'Codable'
这是为什么?我无法修复该错误,并且我尝试了很多变体......
这个问题似乎来自协议,因为如果我删除它,一切都会正常。就像编译器无法看到该协议仅适用于可编码类一样。
您需要符合以下条件的具体类型Codable
,你不能使用符合的协议Codable
.
final public class SourceListHeader<Item: Codable>: Codable {
var name: String = "Give me a name!"
var children = [Item]()
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)