On 7 Sep 2016, at 17:09, Andreas Falkenhahn <[email protected]> wrote:
>
> As a C programmer I'm trying to avoid Objective C whenever and wherever
> possible.
Don’t. Where there’s an Objective-C equivalent, it’ll be less error prone,
shorter to write and easier to debug.
> 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.
All of it can. Objective-C is just C with some syntactic sugar on top.
Really. There’s nothing magic going on. (In fact, the original compilers were
really just preprocessors that churned out C code.)
> Now I'd like to subscribe to the AVPlayerItemDidPlayToEndTimeNotification
> notification. In an Objective C class, this is purportedly done like this:
>
> [[NSNotificationCenter defaultCenter] addObserver:self
> selector:@selector(itemDidFinishPlaying:)
> name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
>
> 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?
Yes, it’s possible, because Objective-C is built on C. Historically (this is
not entirely true now), Objective-C objects were just a struct like this:
struct object {
struct class *isa;
/* Member variables */
};
where the class struct is similar to a C++ vtable in purpose.
So, to get it to call a C function, you’d just build an equivalent data
structure and put the pointer to your C function into the class structure in
the appropriate place.
The Objective-C runtime library has some (C) APIs that do all of these kinds of
things for you and mean you don’t even need to worry about the underlying data
structures (which have, in any case, changed somewhat since the above).
But why not just write it in Objective-C in the first place? The compiler will
then handle all of that *for you*.
If you think Objective-C isn’t fast enough, think again; objc_msgSend() is very
highly optimised and if you have a tight loop where it *really* matters you can
even drop down to using an IMP to call functions directly.
And if it’s just that you don’t want to learn Objective-C, well, the amount of
work you’re doing to avoid it is probably a great deal more than the amount
you’d need to learn enough Objective-C to do whatever it is you’re trying to do.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list ([email protected])
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/archive%40mail-archive.com
This email sent to [email protected]