I want to slice a Data type variable by specifying the memory size. Let's say I want to have only initial 1,000 bytes of an audio file. I'm trying the following code. Both variables point to the same memory address but the sizes differ as expected so seems to work but not sure if it really does.
let original = try! Data(contentsOf: URL(string: "https://example.com/audio.mp3")!)
let sliced = original[0..<1_000] // e.g. Only 1000 bytes (I understand the range can not be beyond the actual original data size)
- Does the above code work as I expect?
- Any concern with the above approach?
- Any better way to realize the same?