如何解决协议扩展中解码器的初始化给出错误“调用'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'在调用'self.init'或分配给'self'之前使用”
解决方法
这个问题是通过在每个当然采用struct的协议中设置标准的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)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。