作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将旧代码转换为使用 SecureCoding 的 Swift 4。苹果文档说这是一个要求,但我的代码无法编译。我尝试过更改 static、func、var 和 class 之间的声明,但是这些都无法转换为 Swift 4。
这是我尝试进行覆盖的代码:
override public class func supportsSecureCoding() -> Bool {
return true
}
显示错误,指出方法未覆盖其父类(super class)中的任何方法。 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
}
}
最佳答案
感谢@Paulw11,答案是将其作为静态变量放入变量中,如下所示:
override public static var supportsSecureCoding: Bool{
return true
}
关于ios - 尝试覆盖supportssecurecoding但无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52710870/
我在创建使用 NSSecureCoding 及其子类的类时遇到问题。 class ClassA: NSObject, NSSecureCoding { public static var su
我是一名优秀的程序员,十分优秀!