Determine if a Data instance is contiguous

Viewed 354

Let d be an instance of Data. In earlier versions of Swift, I could test if it was contiguous in memory using code like

d.enumerateBytes{(pBuf: UnsafeBufferPointer<UInt8>, idx: Data.Index, flag: inout Bool) -> Void in
            if (pBuf.count == d.count) { print("Data is contiguous!") }
        }

However, in Swift 5 enumerateBytes() is deprecated, and I get a warning such as the following:

warning: 'enumerateBytes' is deprecated: use `regions` or `for-in` instead

I'm tempted to do something like

if d.regions.count == 1 { print("Contiguous!!!") }

Yet regions is of type CollectionOfOne<Data>, which by definition always contains one element.

Any suggestions?

1 Answers
Related