• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSData from dispatch_data_t?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSData from dispatch_data_t?


  • Subject: Re: NSData from dispatch_data_t?
  • From: Greg Parker <email@hidden>
  • Date: Tue, 30 Oct 2018 19:49:21 -0700

> On Tue, Oct 30, 2018 at 7:26 PM Carl Hoefs <email@hidden>
> wrote:
>
> Is it possible to create an NSData object from a dispatch_data_t?
>
> I'm interfacing to a 3rd party library that returns only a dispatch_data_t
> object. Once upon a time you could simply cast that to (NSData *) and work
> with it that way, but Xcode now flags it as an error:
>
>    dispatch_data_t data_t = BGRequestSidebandDL(channel);
>    NSData *nsdata = data_t;  // Incompatible pointer types initializing
> 'NSData *' with an
> expression
>        of type 'dispatch_data_t' (aka 'NSObject<OS_dispatch_data> *')

You may use a dispatch_data_t as if it were an NSData (assuming you're on iOS 7
or macOS 10.9 or later). But you do need to cast it explicitly:

    NSData *nsdata = (NSData *)data_t;


> On Oct 30, 2018, at 7:37 PM, Ryan Dignard <email@hidden>
> wrote:
>
> To get the size of the dispatch data you can call
> dispatch_data_get_size(dispatch_data_t) on it which would let you create an
> NSData object.

This may be inefficient. dispatch_data_t often uses a discontiguous
representation. Allocating an NSData this way is would need to copy the data.
(Of course, if the code using it as an NSData goes on to call -bytes instead of
-enumerateByteRangesUsingBlock: then it would be forced to copy into a
contiguous representation anyway.)


--
Greg Parker     email@hidden <mailto:email@hidden>     Runtime
Wrangler


_______________________________________________

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

  • Follow-Ups:
    • Re: NSData from dispatch_data_t?
      • From: Carl Hoefs <email@hidden>
References: 
 >NSData from dispatch_data_t? (From: Carl Hoefs <email@hidden>)
 >Re: NSData from dispatch_data_t? (From: Ryan Dignard <email@hidden>)

  • Prev by Date: Re: NSData from dispatch_data_t?
  • Next by Date: Re: NSData from dispatch_data_t?
  • Previous by thread: Re: NSData from dispatch_data_t?
  • Next by thread: Re: NSData from dispatch_data_t?
  • Index(es):
    • Date
    • Thread