Discussion:
C callbacks with NSNotificationCenter?
(too old to reply)
Jens Alfke
2016-09-07 21:11:23 UTC
Permalink
All of it can. Objective-C is just C with some syntactic sugar on top.
There’s a large amount of semantic sugar too, i.e. the runtime libraries. (If you think the runtime part must be trivial, go read some of Mike Ash’s posts about the implementation of objc_msgsend. That’s some of the most insanely optimized code I’ve ever seen.)
Really. There’s nothing magic going on. (In fact, the original compilers were really just preprocessors that churned out C code.)
You can say this about nearly any compiler, though. Bjarne's original C++ compiler output C code too. The first-generation Go compiler output C code, but Go is absolutely not like sugary C. Most compilers used to output assembly code (nowadays Clang outputs LLVM bitcode), but it seems meaningless to say that those languages are “assembly with some syntactic sugar.”

But yes, you can use the Obj-C runtime API from C to do pretty much anything you can do in Obj-C, like calling methods and implementing classes. It’s just much, much messier, error-prone, and difficult to read compared to writing the damn code in Obj-C.

There’s no pride in refusing to switch languages. I abjured C++ a long time ago, but guess what, my current project is 99% C++ because that language best meets the performance and portability requirements. Next time maybe I’ll be using Swift, or Rust, or Pony. C is an ancient, primitive language and it’s not a good career move to refuse to move past it :)

—Jens
_______________________________________________

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 em
Alastair Houghton
2016-09-07 21:28:59 UTC
Permalink
Post by Jens Alfke
All of it can. Objective-C is just C with some syntactic sugar on top.
There’s a large amount of semantic sugar too, i.e. the runtime libraries. (If you think the runtime part must be trivial, go read some of Mike Ash’s posts about the implementation of objc_msgsend. That’s some of the most insanely optimized code I’ve ever seen.)
Mike’s site is generally a good read for that kind of thing. For those who don’t know:

https://mikeash.com/

Incidentally, on the how-fast-is-objc_msgSend(), according to Mike’s latest Mac figures, it costs on average less than two C++ virtual method calls, and indeed less than division. See

https://mikeash.com/pyblog/friday-qa-2016-04-15-performance-comparisons-of-common-operations-2016-edition.html
Post by Jens Alfke
Really. There’s nothing magic going on. (In fact, the original compilers were really just preprocessors that churned out C code.)
You can say this about nearly any compiler, though.
Sort of, yes, but in the case of Objective-C I think there was a conscious intention to keep the Objective part separate from the C part. Put another way, you could make Objective versions of most other languages, if you wanted, by making similar syntactic additions to them to the ones made for Objective-C, and that looks to me to be a deliberate design decision. That’s not quite the same as C++ or Go, which were always intended to be complete languages of their own.

Anyway, the point I was trying to make (to Andreas) was that he shouldn’t be scared of using Objective-C because it’s going to do all kinds of magic in his program that he doesn’t understand.
Post by Jens Alfke
But yes, you can use the Obj-C runtime API from C to do pretty much anything you can do in Obj-C, like calling methods and implementing classes. It’s just much, much messier, error-prone, and difficult to read compared to writing the damn code in Obj-C.
Absolutely. IMO Andreas really *should* just use Objective-C for the Mac-specific bits of code.
Post by Jens Alfke
There’s no pride in refusing to switch languages. I abjured C++ a long time ago, but guess what, my current project is 99% C++ because that language best meets the performance and portability requirements.
Same here, as it happens. C++11/14 is *much* better than the previous iteration, though it’s still lacking in some areas.
Post by Jens Alfke
Next time maybe I’ll be using Swift, or Rust, or Pony. C is an ancient, primitive language and it’s not a good career move to refuse to move past it :)
Agreed. I’d love to be using Swift right now, actually, but I have portability concerns it doesn’t address and the language itself is still not stable. Both will resolve themselves. Rust and Pony look interesting also.

Kind regards,

Alastair.

--
http://alastairs-place.net


_______________________________________________

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
Graham Cox
2016-09-07 22:47:12 UTC
Permalink
How can I access "IMPORTANT_DATA_PTR" from within the block above?
You already did it. Variables are captured from the scope where the block is declared and “magically” get referenced within the block. So what you’ve written should work.

Have you tried it?

(You might want to more strongly type your parameters though, void* is a nasty idea).

—Graham
_______________________________________________

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.n
Andreas Falkenhahn
2016-09-10 10:49:55 UTC
Permalink
Post by Graham Cox
How can I access "IMPORTANT_DATA_PTR" from within the block above?
You already did it. Variables are captured from the scope where the
block is declared and “magically” get referenced within the block.
So what you’ve written should work.
Have you tried it?
I didn't try it because I thought that the block wouldn't have access to
variables from the outer scope but apparently that isn't the case. It's
indeed working fine. So just using a block that calls the C function is the
perfect solution to my problem. No need for creating a class and using
selectors, I can just use a block that calls a C function. Nice.
Post by Graham Cox
(You might want to more strongly type your parameters though, void* is a nasty idea).
Well, this was just an example of course and the void* just a placeholder.
--
Best regards,
Andreas Falkenhahn mailto:***@falkenhahn.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.narki
Jonathan Mitchell
2016-09-07 16:16:12 UTC
Permalink
Still, I'm wondering: Is it also possible to have NSNotificationCenter call
a C function for me whenever the notification triggers? Can this somehow
be achieved or am I forced to use full Objective C here?
I would go about this by creating a singleton Obj-C object to handle all my notifications.
I would then register C Function pointers with this, perhaps using an NSMapTable or its CF equivalent to track the relationship between the incoming notification and the desired c function.
When the notification comes in I look up my map and call the C function.

HTH

J


_______________________________________________

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
じょいすじょん
2016-09-07 16:33:28 UTC
Permalink
Have you considered Core Foundation?

https://developer.apple.com/library/ios/documentation/CoreFoundation/Reference/CFNotificationCenterRef/
It is all C

Another alternative is to look at the Objective-C runtime library routines.
You can do a lot with the language from C...
As a C programmer I'm trying to avoid Objective C whenever and wherever possible.
The good thing is that I can do most interaction with Cocoa from normal C functions.
I only had to write very few classes. Most of the Cocoa stuff can be done
from normal C functions just fine.
Now I'd like to subscribe to the AVPlayerItemDidPlayToEndTimeNotification
This will call the "itemDidFinishPlaying" method in class "self" whenever
an AVPlayerItem has finished playing.
Of course, I can't use this code in a normal C function because there are
references to "self" and the selector thing doesn't look like it's compatible to
C. So I could just subclass AVPlayerItem and voila, everything's fine.
Still, I'm wondering: Is it also possible to have NSNotificationCenter call
a C function for me whenever the notification triggers? Can this somehow
be achieved or am I forced to use full Objective C here?
--
Best regards,
_______________________________________________
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/dangerwillrobinsondanger%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
d***@gmail.com
2016-09-07 16:49:01 UTC
Permalink
It's not Toll Free bridged.
And Fritz, you contribute so much to folks, nobody would blink if you missed anything.

Sent from my iPhone
“dangerwillrobinsondanger” correctly points to Core Foundation. I’m embarrassed to have forgotten ― it should be very helpful to you, assuming it mixes with AVFoundation.
Post by じょいすじょん
Have you considered Core Foundation?
https://developer.apple.com/library/ios/documentation/CoreFoundation/Reference/CFNotificationCenterRef/
It is all C
Another alternative is to look at the Objective-C runtime library routines.
You can do a lot with the language from C...
As a C programmer I'm trying to avoid Objective C whenever and wherever possible.
The good thing is that I can do most interaction with Cocoa from normal C functions.
I only had to write very few classes. Most of the Cocoa stuff can be done
from normal C functions just fine.
Now I'd like to subscribe to the AVPlayerItemDidPlayToEndTimeNotification
This will call the "itemDidFinishPlaying" method in class "self" whenever
an AVPlayerItem has finished playing.
Of course, I can't use this code in a normal C function because there are
references to "self" and the selector thing doesn't look like it's compatible to
C. So I could just subclass AVPlayerItem and voila, everything's fine.
Still, I'm wondering: Is it also possible to have NSNotificationCenter call
a C function for me whenever the notification triggers? Can this somehow
be achieved or am I forced to use full Objective C here?
--
Best regards,
_______________________________________________

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.narki
Andreas Falkenhahn
2016-09-07 17:46:42 UTC
Permalink
Post by じょいすじょん
Have you considered Core Foundation?
I absolutely love Core Foundation! As you might know I'm currently migrating a Carbon project
to a Cocoa one and lots of it is of course in Core Foundation and I can just re-use those
parts. It's really great that lots of technologies like Core Foundation, Core Audio, Core Text
etc. are still in plain C. Just feels so much better.
--
Best regards,
Andreas Falkenhahn mailto:***@falkenhahn.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.narkiv
Jonathan Mitchell
2016-09-07 16:58:01 UTC
Permalink
Of course, I can't use this code in a normal C function because there are
references to "self" and the selector thing doesn't look like it's compatible to
C. So I could just subclass AVPlayerItem and voila, everything's fine.
I forgot to say that as long as your c function is in a .m file you can create and manipulate Obj-C objects freely.

void testNsObjectInCfunc() {
NSString *q = @"Am I alive";
}

Self is just a pointer to an object so within your c Functions you can cast it to the right type and call methods etc at will.
Obj-C methods are C functions that receive self as a parameter.

Note that using the CF objects is generally more of a challenge than using NSObject objects.

Jonathan
_______________________________________________

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 ge
Andreas Falkenhahn
2016-09-07 17:55:38 UTC
Permalink
As a C programmer I'm trying to avoid Objective C whenever and wherever possible.
I think this might be prone to misunderstanding: What I meant was that I'm trying
to avoid writing classes whenever and wherever possible. I don't mind using
Objective C APIs for getting things done but I always prefer to use Objective C
APIs from plain C functions instead of from classes.

It's just a matter of personal taste. I don't like C++ either. I just prefer
procedural programming in ANSI C. Call me old school ;)
--
Best regards,
Andreas Falkenhahn mailto:***@falkenhahn.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 t
Continue reading on narkive:
Loading...