【问题标题】:Why is my Swift 3 encode running away?为什么我的 Swift 3 编码会跑掉?
【发布时间】:2017-04-25 03:45:52
【问题描述】:

我有一个 Swift 类,它有一些“var”是原始整数值,还有一些是原始整数值数组,或者像 CMTime 这样的结构数组。当我对对象执行 NSKeyedArchiver.archiveRootObject 时,如果我将编码限制为整数值 [CMTime] 和最小的整数数组,则一切正常。 (我可以编码,然后解码,并验证内容的相等性。) 令人费解的是,编码调试器理解的 [UInt16] 成员少于 100M,会导致应用程序在编码期间的内存分配增加到 4Gb,然后是 8Gb,然后是 12Gb(我点击 STOP)。 我对 coder.encode(aSwiftArray, forKey:"aKey") 的使用是否存在一些问题,它有时可以工作,但其他地方不行?

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

【问题讨论】:

  • Edit您的问题与相关代码。
  • 如果需要显示代码上传相关示例

标签: swift decode encode


【解决方案1】:

编码 histPixel 时内存失控的一个可能答案是 encodeObject 正在将数组中的每个 UInt16 值转换为 NSNumber。我曾假设编码只是在幕后使用 encodeBytes。

【讨论】:

    猜你喜欢
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    相关资源
    最近更新 更多