Re: Data, enumerateBytes: separate blocks?
Re: Data, enumerateBytes: separate blocks?
- Subject: Re: Data, enumerateBytes: separate blocks?
- From: Daryle Walker <email@hidden>
- Date: Fri, 22 Dec 2017 11:48:47 -0500
> On Dec 21, 2017, at 11:10 PM, Quincey Morris
> <email@hidden> wrote:
>
> On Dec 21, 2017, at 18:04 , Daryle Walker <email@hidden
> <mailto:email@hidden>> wrote:
>>
>> when multiple blocks are used
>
> At this point, I don’t understand what you don’t understand. Here’s a code
> fragment that works in a playground:
>
>> import Foundation
>>
>> let buffer1: [UInt8] = [1,2]
>> let buffer2: [UInt8] = [3,4,5,6]
>>
>> var data = DispatchData.empty
>> data.append (buffer1.withUnsafeBytes { DispatchData (bytesNoCopy: $0) })
>> data.append (buffer2.withUnsafeBytes { DispatchData (bytesNoCopy: $0) })
>>
>> print (data.count)
>> data.enumerateBytes {
>> bytes, index, stop in
>> print (bytes.count, index, bytes [0])
>> }
>
> producing this output:
>
>> 6
>> 2 0 1
>> 4 2 3
>
> Isn’t this what you were asking for: a loop enumerating each of the
> contiguous portions of a larger data set?
I already have the code as an extension of Data. In C, we can use pointer
type-punning shenanigans to convert between a dispatch_data_t and NSData*. To
trigger this code in a test that I would write in Swift, DispatchData would
need to be convertible to Data. Is there a way to do the conversion in Swift?
It doesn’t seem obvious since DispatchData and Data are value types, not
pointer nor reference types.
I just checked it out with your code above in a playground. I added an
extension to Data called “printHello” and called it with a Data object I also
added. Sure enough, the DispatchData object could not call that same method.
—
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden