我在以下类中遇到上述编译器错误:
class Log: NSObject, NSCoding {
var targetHoursPerWeek: Double
var weeksLog: Double[]
// Serialization keys that are using to implement NSCoding.
struct SerializationKey {
static let targetHoursPerWeek = "targetHoursPerWeek"
static let weeksLog = "weeksLog"
}
init() {
targetHoursPerWeek = 7.0
weeksLog = Double[](count: 7, repeatedValue: 0.0)
// [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
}
// MARK: NSCoding
init(coder decoder: NSCoder) {
targetHoursPerWeek = decoder.decodeObjectForKey(SerializationKey.targetHoursPerWeek) as Double
weeksLog = decoder.decodeObjectForKey(SerializationKey.weeksLog) as Double[]
}
func encodeWithCoder(encoder: NSCoder) {
encoder.encodeObject(targetHoursPerWeek, forKey: SerializationKey.targetHoursPerWeek)
encoder.encodeObject(weeksLog, forKey: SerializationKey.weeksLog)
}
}
我认为错误来自
行 weeksLog = decoder.decodeObjectForKey(SerializationKey.weeksLog) as Double[]
但是如果是这样的话我感到难过 - “Double []”同意“weekLog”的声明。 所以我很难过!
答案 0 :(得分:0)
<强>更新强>
我将weeksLog更改为可选项并更改为?并编译。
var weeksLog: Double[]?
和
weeksLog = decoder.decodeObjectForKey(SerializationKey.weeksLog) as? Double[]