gpt4 book ai didi

ios - 尝试覆盖supportssecurecoding但无法编译

转载 作者:行者123 更新时间:2023-11-30 11:06:03 26 4
gpt4 key购买 nike

我正在将旧代码转换为使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com