设d是数据的一个实例。在Swift的早期版本中,我可以使用如下代码测试它在内存中是否连续
d.enumerateBytes{(pBuf: UnsafeBufferPointer<UInt8>, idx: Data.Index, flag: inout Bool) -> Void in
if (pBuf.count == d.count) { print("Data is contiguous!") }
}然而,在Swift 5中,enumerateBytes()已被弃用,我收到如下警告:
warning: 'enumerateBytes' is deprecated: use `regions` or `for-in` instead我很想做像这样的事情
if d.regions.count == 1 { print("Contiguous!!!") }然而,regions的类型是CollectionOfOne<Data>,根据定义,它总是包含一个元素。
有什么建议吗?
发布于 2019-10-04 11:14:27
从Swift 5开始,所有Data值都有连续的存储,类型符合新的ContiguousBytes协议(在#20225中实现)。作为@matt points out,此更改是highlighted in a recent WWDC talk
所以从Swift 5开始,我们保证结构数据是一个连续的缓冲区类型。
https://stackoverflow.com/questions/58229364
复制相似问题