使类可编码

时间:2019-06-25 16:35:45

标签: swift codable decodable

我有一个可编码的协议和一个可编码的类:

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类一样。

1 个答案:

答案 0 :(得分:1)

您需要符合 mh_terms = ['mental', 'ptsd', 'sud', 'substance abuse', 'drug abuse', 'alcohol', 'alcoholism', 'anxiety', 'depressing', 'bipolar', 'mh', 'smi', 'oud', 'opioid' ] singleabstract = 'This is a research abstract that includes words like mental health and anxiety. My hope is that I get my code to work and not resort to alcohol.' for mh in mh_terms: mh = mh.lower mh = str(mh) number_of_occurences = 0 for word in singleabstract.split(): if mh in word: number_of_occurences += 1 print(number_of_occurences) 的具体类型,不能使用符合Codable的协议。

Codable