Discussion:
NSData from dispatch_data_t?
Carl Hoefs
2018-10-31 02:26:05 UTC
Permalink
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> *')

Nor can I do this:
NSData *nsdata = [[NSData alloc] initWithBytes:data_t length:size];
because I don't know the size of the dispatch_data_t returned to me.

Thx,
-Carl

_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Ryan Dignard
2018-10-31 02:37:26 UTC
Permalink
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.

https://developer.apple.com/documentation/dispatch/1452977-dispatch_data_get_size?language=objc

-Ryan
Post by Carl Hoefs
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
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> *')
NSData *nsdata = [[NSData alloc] initWithBytes:data_t length:size];
because I don't know the size of the dispatch_data_t returned to me.
Thx,
-Carl
_______________________________________________
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
https://lists.apple.com/mailman/options/cocoa-dev/conceptuallyflawed%40gmail.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Carl Hoefs
2018-10-31 02:48:05 UTC
Permalink
It's ugly, but it compiles:

[NSData dataWithBytes:(__bridge const void * _Nullable)(data_t) length:dispatch_data_get_size(data_t)];

Thanks!
-Carl
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.
https://developer.apple.com/documentation/dispatch/1452977-dispatch_data_get_size?language=objc
-Ryan
Is it possible to create an NSData object from a dispatch_data_t?
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> *')
NSData *nsdata = [[NSData alloc] initWithBytes:data_t length:size];
because I don't know the size of the dispatch_data_t returned to me.
Thx,
-Carl
_______________________________________________
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
https://lists.apple.com/mailman/options/cocoa-dev/conceptuallyflawed%40gmail.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Greg Parker
2018-10-31 02:51:19 UTC
Permalink
Post by Carl Hoefs
[NSData dataWithBytes:(__bridge const void * _Nullable)(data_t) length:dispatch_data_get_size(data_t)];
This is incorrect: it creates an NSData object whose contents are the dispatch_data_t object header. That is not going to match the dispatch_data_t's actual contents.
--
Greg Parker ***@apple.com <mailto:***@apple.com> Runtime Wrangler


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Greg Parker
2018-10-31 02:49:21 UTC
Permalink
Post by Carl Hoefs
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
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;
Post by Carl Hoefs
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 ***@apple.com <mailto:***@apple.com> Runtime Wrangler


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Carl Hoefs
2018-10-31 02:53:35 UTC
Permalink
Post by Greg Parker
NSData *nsdata = (NSData *)data_t;
Yes! That works! (I thought I had tried the explicit cast, but I guess I hadn't...)

Thanks!!
-Carl


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Tom Doan
2018-11-07 05:51:53 UTC
Permalink
I just started testing a port of my application to Mojave. I'm having a
rather odd problem with NSString drawAtPoint withAttributes. I use
that to add text to graph windows. All the lines and fills look fine, but
the text, done with drawAtPoint, doesn't show on the screen.
However, if I take the window and create a PDF from it, everything
looks fine. It also works fine on High Sierra. Anybody have any idea
what might be going wrong with this?

Best regards,

Tom Doan
---
Estima
1560 Sherman Ave #1029
Evanston, IL 60201
USA


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Ken Thomases
2018-11-07 06:09:10 UTC
Permalink
Post by Tom Doan
I just started testing a port of my application to Mojave. I'm having a
rather odd problem with NSString drawAtPoint withAttributes. I use
that to add text to graph windows. All the lines and fills look fine, but
the text, done with drawAtPoint, doesn't show on the screen.
However, if I take the window and create a PDF from it, everything
looks fine. It also works fine on High Sierra. Anybody have any idea
what might be going wrong with this?
Are you just running your app on Mojave or are you also building against the 10.14 SDK? Are you able to test a version of your app that was built against an earlier SDK? Does that show the same issue when run on Mojave?

Where/when are you calling -drawAtPoint:withAttributes:? Is it within a view's -drawRect: method? Or are you trying to draw at some other time?

Does the "Layer-Backed Views" section of the AppKit Release Notes for 10.14 explain what you're seeing?
<https://developer.apple.com/documentation/macos_release_notes/macos_mojave_10_14_release_notes/appkit_release_notes_for_macos_10_14#3014921>

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Tom Doan
2018-11-07 17:40:22 UTC
Permalink
Post by Ken Thomases
Post by Tom Doan
I just started testing a port of my application to Mojave. I'm
having a rather odd problem with NSString drawAtPoint
withAttributes. I use that to add text to graph windows. All the
lines and fills look fine, but the text, done with drawAtPoint,
doesn't show on the screen. However, if I take the window and create
a PDF from it, everything looks fine. It also works fine on High
Sierra. Anybody have any idea what might be going wrong with this?
Are you just running your app on Mojave or are you also building
against the 10.14 SDK?
I built it in XCode 10.1. I don't see an option on that to choose the
SDK---all it shows is "Mac OSX" as the Base SDK.

Are you able to test a version of your app
Post by Ken Thomases
that was built against an earlier SDK? Does that show the same issue
when run on Mojave?
If I run a build from before XCode 10.1, it works fine.
Post by Ken Thomases
Where/when are you calling -drawAtPoint:withAttributes:? Is it within
a view's -drawRect: method? Or are you trying to draw at some other
time?
Yes, in the drawRect.
Post by Ken Thomases
Does the "Layer-Backed Views" section of the AppKit Release Notes for
10.14 explain what you're seeing?
<https://developer.apple.com/documentation/macos_release_notes/macos_m
ojave_10_14_release_notes/appkit_release_notes_for_macos_10_14#3014921
I'm not really sure how. As I said, any non-text information seems to
be drawn correctly. Why would only the text not get drawn
properly?
Post by Ken Thomases
Regards,
Ken
---
1560 Sherman Ave #1029
Evanston, IL 60201
USA


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Ken Thomases
2018-11-07 18:47:58 UTC
Permalink
Post by Tom Doan
Post by Ken Thomases
Does the "Layer-Backed Views" section of the AppKit Release Notes for
10.14 explain what you're seeing?
<https://developer.apple.com/documentation/macos_release_notes/macos_m
ojave_10_14_release_notes/appkit_release_notes_for_macos_10_14#3014921
I'm not really sure how. As I said, any non-text information seems to
be drawn correctly. Why would only the text not get drawn
properly?
Well, are those all drawn in the same view? Are you perhaps using -setNeedsDisplayInRect: with rects for the non-text stuff and accidentally relying on an enclosing/overlapping/underlapping view's redraw to force the drawing of the text?

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net
Tom Doan
2018-11-07 19:31:16 UTC
Permalink
Post by Ken Thomases
Post by Tom Doan
Post by Ken Thomases
Does the "Layer-Backed Views" section of the AppKit Release Notes
for 10.14 explain what you're seeing?
<https://developer.apple.com/documentation/macos_release_notes/maco
s_m
ojave_10_14_release_notes/appkit_release_notes_for_macos_10_14#3014
921
I'm not really sure how. As I said, any non-text information seems
to be drawn correctly. Why would only the text not get drawn
properly?
Well, are those all drawn in the same view? Are you perhaps using
-setNeedsDisplayInRect: with rects for the non-text stuff and
accidentally relying on an enclosing/overlapping/underlapping view's
redraw to force the drawing of the text?
I do setNeedsDisplay for the whole view.
Post by Ken Thomases
Regards,
Ken
---
1560 Sherman Ave #1029
Evanston, IL 60201
USA


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.net

Loading...