swift - 协议扩展中解码器的初始化给出了错误“在'self.init'调用或分配给'self'之前使用'self'”
问题描述
我想解码协议的扩展:
protocol SettingsContentProtocol: Codable {
var audioDelegate:AudioDelegate? { get set }
var isPlaying: Bool { get set }
var meditationTimes: [MeditationItem: TimeInterval] {get set}
var intermediate: Int {get set}
var contentsDuration: Float {get set}
var durata:TimeInterval {get set}
var parzialeDurata:TimeInterval {get set}
var date: Date {get set}
var hour: Int {get}
var followDuration: TimeInterval {get set}
func selectPeriod(item:MeditationItem)
mutating func sliderMoved(item: MeditationItem, sliderPosition: Float)
func calculateContentsDuration()->Float
func tooShortTimeForContents(completion:@escaping ((Bool)->Void))
func cleanQueue(items:[MPMediaItem])
func selectRow(indexPath: IndexPath)
func tableEdit(indexPath: IndexPath)
func rowAt(indexPath: IndexPath, tableView: UITableView)->UITableViewCell
mutating func adjustProgress(progress: TimeInterval)
mutating func prepareMeditation()
func meditationDetails()->Meditation
func meditationMessage(final: Bool)->String
func meditationTime()->String
}
和:
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
isPlaying = try container.decode(Bool.self, forKey: .isPlaying)
meditationTimes = try container.decode([MeditationItem: TimeInterval].self, forKey: .meditationTimes)
intermediate=try container.decode(Int.self, forKey: .intermediate)
contentsDuration=try container.decode(Float.self, forKey: .contentsDuration)
durata=try container.decode(TimeInterval.self, forKey: .durata)
parzialeDurata=try container.decode(TimeInterval.self, forKey: .parzialeDurata)
date=try container.decode(Date.self, forKey: .date)
followDuration=try container.decode(TimeInterval.self, forKey: .followDuration)
}
然而,当我编译时,我得到每一行错误:
“在‘self.init’调用或分配给‘self’之前使用‘self’”
我发现的所有示例似乎都与我的代码一致,但可能有什么问题?
解决方案
这个问题是通过在每个采用结构当然采用的协议中设置标准 init() 来解决的,我从 init(from:) 中调用它如下:
func encode(to encoder: Encoder) throws{
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(isPlaying, forKey: .isPlaying)
try container.encode(meditationTimes, forKey: .meditationTimes)
try container.encode(intermediate, forKey: .intermediate)
try container.encode(durata, forKey: .durata)
try container.encode(parzialeDurata, forKey: .parzialeDurata)
try container.encode(date, forKey: .date)
try container.encode(followDuration, forKey: .followDuration)
}
init(from decoder: Decoder) throws {
self.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
isPlaying = try container.decode(Bool.self, forKey: .isPlaying)
meditationTimes = try container.decode([MeditationItem: TimeInterval].self, forKey: .meditationTimes)
intermediate=try container.decode(Int.self, forKey: .intermediate)
durata=try container.decode(TimeInterval.self, forKey: .durata)
parzialeDurata=try container.decode(TimeInterval.self, forKey: .parzialeDurata)
date=try container.decode(Date.self, forKey: .date)
followDuration=try container.decode(TimeInterval.self, forKey: .followDuration)
}
推荐阅读
- git - gitginore 无法忽略文件夹,添加时出错
- google-cloud-platform - 如何使用 go 在 GCP Pub/Sub 中进行推送订阅?
- hazelcast - Hazelcast 在客户端-服务器拓扑中从 RDBMS 加载数据
- deep-learning - 为什么在使用暗网更新 yolov4 时出现无法打开文件:/home/hp/computerVision/backup//yolov4_final.weights?
- javascript - 查找父母的父母的img src
- macos - NSTextView 在放置在 NSWindow 顶部附近时不起作用
- laravel - 使用带有 Laravel 的 Gmail SMTP 发送电子邮件时出错
- excel - 浏览器中带有宏的 Excel
- python - 更高效的双 For 循环超过 70k 个变量?
- javascript - 复制一个工作表中的选定列并将它们添加到另一个工作表中的选定列