I have a Swift class that seems to work fine, except when I try to implement NSCoding. The line that seems to be giving me an issue is:
aCoder.encodeObject(runSamples, forKey: "runSamples")
If I comment this line out, everything compiles fine. However, uncommenting it in Xcode results in:
Bitcast requires both operands to be pointer or neither %201 = bitcast i32 %200 to %objc_object*, !dbg !241 LLVM ERROR: Broken function found, compilation aborted! Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1
Uncommenting the line Playground just causes the app to crash.
I assume this is a compiler issue, but I want to see if there was something I'm obviously missing before reporting to Apple.
Full class:
class RunRecord: NSObject {
let startDate: NSDate
var endDate: NSDate?
var runSamples: CLLocation[]
var currentSpeed: CLLocationDistance {
return self.runSamples[runSamples.endIndex - 1].speed
}
init(startDate: NSDate) {
self.startDate = startDate
self.runSamples = CLLocation[]()
super.init()
}
init(coder aDecoder: NSCoder!) {
startDate = aDecoder.decodeObjectForKey("startDate") as NSDate
endDate = aDecoder.decodeObjectForKey("endDate") as? NSDate
runSamples = aDecoder.decodeObjectForKey("runSamples") as CLLocation[]
}
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeObject(startDate, forKey: "startDate")
aCoder.encodeObject(endDate, forKey: "endDate")
aCoder.encodeObject(runSamples, forKey: "runSamples")
}
func addSample(sample: CLLocation) {
runSamples.append(sample)
}
}
Looks like the problem is that the Swift array was being automatically bridged to Objective-C. This fixes the issue:
aCoder.encodeObject(runSamples.bridgeToObjectiveC(), forKey: "runSamples")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments