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
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.
© 2024 OneMinuteCode. All rights reserved.