Re: Mixing Obj-C and C "methods"
Re: Mixing Obj-C and C "methods"
- Subject: Re: Mixing Obj-C and C "methods"
- From: Jean-Daniel Dupas <email@hidden>
- Date: Tue, 30 Jul 2013 10:44:46 +0200
Le 30 juil. 2013 à 10:27, Vincent Habchi <email@hidden> a écrit :
> 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.
If it is in the implementation block, you can access all private ivar and property, but only if you have a reference to self in the first place.
For instance, you can have this:
@implementation Foo {
id privateVar;
}
static inline void internalInlineFunction(Foo *self) {
// do something with self->privateVar.
}
- (void)publicMethod {
internalInlineFunction(self);
}
@end
I'm using this trick when I need inline code (something that can't be done with Obj-C method), but for a callback, I would rather just keep it simple and simply call a method to handle it.
@implementation Foo
static void callback(void *ctxt) {
// Sidenote; 'self' is a reserved keyword only inside Obj-C method body. You can use it freely elsewhere, and it does not have special meaning.
// that's why you have to pass it as explicit parameter.
Foo *self = (Foo *)ctxt;
[self handleCallback];
}
- (void)handleCallback {
//
}
@end
>
>> 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?
>
> Thanks a lot!
> Vincent
>
>
> _______________________________________________
>
> 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
-- Jean-Daniel
_______________________________________________
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