0

I am getting the above compiler error on the following class:

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)
}

}

I believe that the error comes from the line

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

but if so I am stumped -- the "as Double[]" agrees with the declaration of "weeksLog". So I am stumped!

jxxcarlson
  • 223
  • 3
  • 13
  • I reproduced this and read the complete compiler error message. Something is going wrong there at a lower level. So this is a bug for sure. You should report it. – Jens Wirth Jun 15 '14 at 18:47
  • But you are wrong: The compiler runs fine if you remove this line: encoder.encodeObject(weeksLog, forKey: SerializationKey.weeksLog). Maybe you can write a workaround that does not cause the compiler to make trouble. – Jens Wirth Jun 15 '14 at 18:49
  • I will give the workaround a try. Do you have suggestions? – jxxcarlson Jun 15 '14 at 21:18
  • Thanks for pinpointing the error location. That gives me something to work with. – jxxcarlson Jun 15 '14 at 21:19
  • This now works: encoder.encodeObject(weeksLog as AnyObject[], forKey: SerializationKey.weeksLog) Thanks!! – jxxcarlson Jun 15 '14 at 21:22

1 Answers1

0

Updated

I changed weeksLog to be an optional and changed as to as? and it compiles.

var weeksLog: Double[]?

and

weeksLog = decoder.decodeObjectForKey(SerializationKey.weeksLog) as? Double[]
Jonathan Parker
  • 6,705
  • 3
  • 43
  • 54