I want to extract NSData from NSData

Asked 2 years ago, Updated 2 years ago, 34 views

If you want to specify a range and truncate the passed "NSData data",

 varwork:UInt8=0x00
data.getBytes (&work,range:NSMakeRange)

You can get it by doing the above, but if you want to cut some NSData out of NSData,

 varwork: [UInt8] = Ensure size to cut
data.getBytes (&work,range:NSMakeRange)

Is the only way to convert work to NSData after this?

swift

2022-09-30 16:32

1 Answers

Take a quick look at the NSData reference.
NSData Class Reference
You don't have to read the details until you see a similar method name.

-subdataWithRange:
substring If you know that you can extract a part of the string with a method name, you can imagine that this method can extract a part of the data.I thought it was a hit, but let's look at the contents and try it on Playground.

let bytes: [UInt8] = [0x01, 0x02, 0x03, 0x04]
let data = NSData(bytes:bytes, length:bytes.count) //->01020304>
let subdata=data.subdataWithRange(NSMakeRange(1,2))//-><0203>

I think it's working as you expect.

By the way, if you use Swift, you can also use the range operator to create NSRange.
NSRange is the same as NSMakeRange, so the example above is

let subdata=data.subdataWithRange (NSRange(1..<3))

It will be the same even if it is written thatTry it.


2022-09-30 16:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.