Re: Mixing Obj-C and C "methods"
Re: Mixing Obj-C and C "methods"
- Subject: Re: Mixing Obj-C and C "methods"
- From: Rick Mann <email@hidden>
- Date: Tue, 30 Jul 2013 01:35:58 -0700
On Jul 30, 2013, at 01:27 , Vincent Habchi <email@hidden> wrote:
> Rick,
>
> thanks for answering, because what I found on the Internet seems contradictory. Some say that if the C function is placed inside the implementation block, then it can access attributes as if it were a true Obj-C method; some say otherwise. So it’s a bit difficult to find a definitive answer thereon.
>
>> Having said that, most callback APIs allow you to pass a context parameter that gets passed back to your C callback. Often times, this context parameter is a void* you pass in along with a pointer to your callback function. You can pass "self" in this parameter when you register the callback, then cast it inside your callback back to MyClass* (or whatever your class is).
>
> Yes, right; it’s a SQLite callback, the first parameter is a void *. I wanted to pass a pointer to a structure containing both a unique query id (out of uuid) and a pointer to self, but got told off by ARC because it apparently forbids to embed pointers to Obj-C objects in C-structs. So I just bridge-cast it to void *.
>
> Assuming the pointer to the struct is named ‘info’ and the field containing a reference to ‘self’ is called ‘this’, [info->this someMethod] as well as info->this->someAttribute are legal, aren’t they?
I think you can embed the pointer, but you might have to qualify it with something to make ARC happy (it probably can't auto-nil it inside a C struct, so you have to use one of the other storage modifiers).
Alternatively, you can put your additional metadata in your object, and just pass self with appropriate ARC bridging (almost certainly no retain/transfer of ownership).
--
Rick
_______________________________________________
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