本文介绍了我怎样才能得到一个NS codeR为en code /德code结构的斯威夫特阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象必须符合 NSCoding ,并保存 UINT64 值的数组。我怎么能连接所有code /解code将其与 NS codeR ?奖金的问题:我怎么能带code是最紧凑? (它进入保存的游戏中心的状态数据,其大小是有限的。)

在理想情况下,我只想写一个内部这是数组的大小 N ,然后写 N 时代的 UINT64 ,同样读它的64位。我可以这样做吗?

coder.en codeObject的(值,forKey:V)。不起作用

 类为MyObject:NSCoding {    私人VAR值:[UINT64]    // ...    // MARK: -  NSCoding    所需的init(codeR德codeR:NS codeR){
        //?
    }    FUNC EN codeWith codeR(codeR:NS codeR){
        //?
    }
}


解决方案

下面是一个可能的解决方案,EN codeS的 UINT64 数组
字节数组。它由答案启发如何序列化数组c与NSCoding?

 类为MyObject:NSObject的,NSCoding {    变种的值:[UINT64] = []    的init(值:[UINT64]){
        self.values​​ =值
    }    // MARK: - NSCoding
    所需的init(codeR德codeR:NS codeR){
        super.init()
        变种数= 0
        //德codeBytesForKey()返回一个UnsafePointer< UINT8>中指向不可改变的数据。
        让PTR =去coder.de codeBytesForKey(价值观,returnedLength:放大器;计数)
        //如果我们把它转换为相应类型的缓冲区指针和计数...
        让BUF = UnsafeBufferPointer<&UINT64 GT;(启动:UnsafePointer(PTR),计数:计数/的sizeof(UINT64))
        // ...那么数组创建变得容易。
        值=阵列(BUF)
    }    FUNC EN codeWith codeR(codeR:NS codeR){
        //这个连接codeS两个字节数和数据本身。
        coder.en codeBytes(UnsafePointer(值),长度:values​​.count *的sizeof(UINT64),forKey:价值)
    }
}

测试:

 令obj =为MyObject(值:[1,2,3,UInt64.max])
让数据= NSKeyedArchiver.archivedDataWithRootObject(OBJ)让DEC = NSKeyedUnarchiver.unarchiveObjectWithData(数据)!为MyObject
的println(dec.values​​)// [1,2,3,18446744073709551615]

I have an object that must conform to NSCoding and that holds an array of UInt64 values. How can I encode/decode it with an NSCoder at all? Bonus question: how can I encode it most compactly? (It has to go into saved Game Center state data, whose size is limited.)

Ideally, I just want to write an Int which is the size n of the array, and then write n times the 64 bits of a UInt64, and read it similarly. Can I do this?

coder.encodeObject(values, forKey: "v") doesn't work.

class MyObject: NSCoding {

    private var values: [UInt64]

    // …

    // MARK: - NSCoding

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

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


}
解决方案

Here is a possible solution that encodes the UInt64 array asan array of bytes. It is inspired by the answers to How to serialize C array with NSCoding?.

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

Test:

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

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

这篇关于我怎样才能得到一个NS codeR为en code /德code结构的斯威夫特阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:25