我正在将较旧的代码转换为使用SecureCoding的Swift 4。 Apple文档说这是一项要求,但是我的代码无法编译。我尝试过在static,func,var和class之间更改声明,但是这些声明都无法转换为Swift 4。
这是我尝试进行覆盖的代码:
override public class func supportsSecureCoding() -> Bool {
return true
}
显示错误,表明方法未覆盖其父类中的任何方法。 Reference Doc
这是包含解决方案的完整更新代码:
import CareKit
public class ZCCareMonthlySchedule : OCKCareSchedule {
var calendar: NSCalendar?
public class func monthlyScheduleWithStartDate(startDate: NSDateComponents, occurrencesFromJanuaryToDecember: [NSNumber], monthsToSkip: UInt, endDate: NSDateComponents?) -> ZCCareMonthlySchedule? {
guard occurrencesFromJanuaryToDecember.count == 12
else { return nil}
//TODO: Requires fixing after CareKit is updated to handle sub classes
// let schedule = super.initWithStartDate(startDate: startDate, endDate: endDate, occurrences: occurrencesFromJanuaryToDecember, timeUnitsToSkip: monthsToSkip)
return nil
}
override public var type: OCKCareScheduleType {
return OCKCareScheduleType.other
}
override public func numberOfEvents(onDate date: DateComponents) -> UInt {
calendar = NSCalendar.init(calendarIdentifier: NSCalendar.Identifier.gregorian)
calendar!.timeZone = NSTimeZone(abbreviation: "UTC")! as TimeZone
let startMonth = calendar?.ordinality(of: NSCalendar.Unit.month, in: NSCalendar.Unit.era, for: self.startDate.date! )
let endMonth = calendar?.ordinality(of: NSCalendar.Unit.month, in: NSCalendar.Unit.era, for: date.date! )
let monthsSinceStart = startMonth! - endMonth!
let month = calendar?.component(NSCalendar.Unit.month, from: date.date!)
//TODO: Add a unit test to verify this works
let occurrences : UInt = ((UInt(monthsSinceStart) % (self.timeUnitsToSkip + 1)) == 0) ? self.occurrences[month!-1].uintValue : 0;
return occurrences;
}
//MARK: NSSecureCoding Support
override public static var supportsSecureCoding: Bool{
return true
}
required convenience public init?(coder aDecoder: NSCoder) {
self.init(coder: aDecoder)
}
//MARK: NSCopying Support
override public func copy(with zone: NSZone?) -> Any {
let theCopy = super.copy(with: zone) as! ZCCareMonthlySchedule
return theCopy
}
}
答案 0 :(得分:0)
感谢@ Paulw11,答案是将其作为静态var放入变量中,例如:
override public static var supportsSecureCoding: Bool{
return true
}