Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read partially a Data object

Tags:

swift

nsdate

The recent change in Swift 4 provides the simple way to initialize a byte array with your Data object. The result gets you an [UInt8] with the whole data stored into it.

let array = [UInt8](data)

I can't find the solution to load the same Data object only partially using an offset and a length. Is it possible without slicing the whole array or should I switch to InputStream?

like image 343
John Difool Avatar asked Oct 15 '25 15:10

John Difool


1 Answers

You can slice a Data object with a subscript.

For example, you just want the 3rd to 5th index in the data, you would use

data[3..<6]

In your case, you would do

let array = [UInt8](data[lowerIndex..<upperIndex])

where lowerIndex and upperIndex are the indexes.

like image 116
Papershine Avatar answered Oct 18 '25 10:10

Papershine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!