Swift编译器错误:Bitcast要求两个操作数都是指针

时间:2014-06-15 15:33:39

标签: ios swift nscoding

我在以下类中遇到上述编译器错误:

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”的声明。 所以我很难过!

1 个答案:

答案 0 :(得分:0)

<强>更新

我将weeksLog更改为可选项并更改为?并编译。

var weeksLog: Double[]?

weeksLog = decoder.decodeObjectForKey(SerializationKey.weeksLog) as? Double[]