2017-04-25 33 views
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) 
    }   
+0

[编辑]带有相关代码的问题。如果需要, – rmaddy

+0

显示代码上传相关示例 –

回答

0

一种可能的答案存储器失控而编码histPixel是encodeObject在阵列到一个NSNumber在将每个UINT16值。我曾假设编码只是在幕后使用encodeBytes。

相关问题