簡體   English   中英

如何獲得NSCoder來編碼/解碼Swift結構數組?

[英]How can I get an NSCoder to encode/decode a Swift array of structs?

我有一個必須符合NSCoding的對象,並且該NSCoding包含UInt64值的數組。 如何使用NSCoder對其進行編碼/解碼? 額外的問題:如何最緊湊地編碼? (它必須進入已保存的Game Center狀態數據,其大小是有限的。)

理想情況下,我只想寫的Int它的大小n陣列的,然后寫n倍的64位UInt64 ,並且類似地讀取它。 我可以這樣做嗎?

coder.encodeObject(values, forKey: "v")不起作用。

class MyObject: NSCoding {

    private var values: [UInt64]

    // …

    // MARK: - NSCoding

    required init(coder decoder: NSCoder) {
        // ???
    }

    func encodeWithCoder(coder: NSCoder) {
        // ???
    }


}

這是將UInt64數組編碼為字節數組的可能解決方案。 它的靈感來自於如何使用NSCoding序列化C數組?

class MyObject: NSObject, NSCoding {

    var values: [UInt64] = []

    init(values : [UInt64]) {
        self.values = values
    }

    // MARK: - NSCoding
    required init(coder decoder: NSCoder) {
        super.init()
        var count = 0
        // decodeBytesForKey() returns an UnsafePointer<UInt8>, pointing to immutable data.
        let ptr = decoder.decodeBytesForKey("values", returnedLength: &count)
        // If we convert it to a buffer pointer of the appropriate type and count ...
        let buf = UnsafeBufferPointer<UInt64>(start: UnsafePointer(ptr), count: count/sizeof(UInt64))
        // ... then the Array creation becomes easy.
        values = Array(buf)
    }

    func encodeWithCoder(coder: NSCoder) {
        // This encodes both the number of bytes and the data itself.
        coder.encodeBytes(UnsafePointer(values), length: values.count * sizeof(UInt64), forKey: "values")
    }
}

測試:

let obj = MyObject(values: [1, 2, 3, UInt64.max])
let data = NSKeyedArchiver.archivedDataWithRootObject(obj)

let dec = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! MyObject
print(dec.values) // [1, 2, 3, 18446744073709551615]

Swift 3(Xcode 8)更新:

class MyObject: NSObject, NSCoding {

    var values: [UInt64] = []

    init(values : [UInt64]) {
        self.values = values
    }

    // MARK: - NSCoding
    required init(coder decoder: NSCoder) {
        super.init()
        var count = 0
        // decodeBytesForKey() returns an UnsafePointer<UInt8>?, pointing to immutable data.
        if let ptr = decoder.decodeBytes(forKey: "values", returnedLength: &count) {
            // If we convert it to a buffer pointer of the appropriate type and count ...
            let numValues = count / MemoryLayout<UInt64>.stride
            ptr.withMemoryRebound(to: UInt64.self, capacity: numValues) {
                let buf = UnsafeBufferPointer<UInt64>(start: UnsafePointer($0), count: numValues)
                // ... then the Array creation becomes easy.
                values = Array(buf)
            }
        }
    }

    public func encode(with coder: NSCoder) {
        // This encodes both the number of bytes and the data itself.
        let numBytes = values.count * MemoryLayout<UInt64>.stride
        values.withUnsafeBufferPointer {
            $0.baseAddress!.withMemoryRebound(to: UInt8.self, capacity: numBytes) {
                coder.encodeBytes($0, length: numBytes, forKey: "values")
            }
        }
    }
}


let obj = MyObject(values: [1, 2, 3, UInt64.max])
let data = NSKeyedArchiver.archivedData(withRootObject: obj)

let dec = NSKeyedUnarchiver.unarchiveObject(with: data) as! MyObject
print(dec.values) // [1, 2, 3, 18446744073709551615]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM