0
我有一个Swift类,它有一些“var”是原始的整数值,有些是原始的整数值数组,或者是像CMTime这样的结构数组。当我在对象上执行NSKeyedArchiver.archiveRootObject时,如果将编码限制为整数值[CMTime]和最小的整数数组,则一切正常。 (我可以编码,然后解码并验证内容是否相等) 令人费解的是编码调试器能够理解的[UInt16]的成员少于100M,导致应用在编码过程中的内存分配增加到4Gb ,然后是8Gb,然后是12Gb(我打STOP的地方)。 是否有一些问题与我使用coder.encode(aSwiftArray,forKey:“aKey”),它有时会工作,但不是其他人?为什么我的Swift 3编码失控?
typealias WordSize = UInt16
class PixelHistogram :NSObject, NSCoding {
var pixelCount:Int = 0
var timeAtFrame: [CMTime] = [] // CMTime for each frame
var pixelVsDelta: [UInt] = [] // 256x256 histogram of pixel vs DeltaPixel
var histPixel:[WordSize] = [] // histogram of Y at each pixel [pixelCount][256]
}
// Initializing the various arrays...
histPixel = [WordSize](repeating: 0, count: 256*pixelCount)
pixelVsDelta = [UInt](repeating: 0, count: 256*256)
required convenience init?(coder aDecoder:NSCoder){
self.init()
timeAtFrame = aDecoder.decodeObject(forKey:Slot.timeAtFrame.rawValue) as! [CMTime]
pixelVsDelta = aDecoder.decodeObject(forKey:Slot.pixelVsDelta.rawValue) as! [UInt]
func encode(with coder:NSCoder)
coder.encode(timeAtFrame, forKey: Slot.timeAtFrame.rawValue)
coder.encode(pixelVsDelta, forKey: Slot.pixelVsDelta.rawValue)
}
[编辑]带有相关代码的问题。如果需要, – rmaddy
显示代码上传相关示例 –