Re: Mixing Obj-C and C "methods"
Re: Mixing Obj-C and C "methods"
- Subject: Re: Mixing Obj-C and C "methods"
- From: Andy Lee <email@hidden>
- Date: Tue, 30 Jul 2013 11:26:11 -0400
On Jul 30, 2013, at 4:26 AM, Michael Crawford <email@hidden> wrote:
> However, I expect there is a way you could call an Objective-C method
> from vanilla C. Possibly you will need some assembly-language glue.
The nice thing is, you don't need glue. You can send Objective-C messages from within C, as long as the code is compiled as Objective-C or Objective-C++. All you need is...
> You will need some way for your C callback to be told what "self" is.
...bearing in mind that within the C function it doesn't have to be called "self". It *can* be, if that makes things clearer, but as Jean-Daniel mentioned, "self" has no special meaning within a C function. Depending on the situation, "self" might be a *more* confusing name; personally, I would use something else in most if not all cases but that may be a matter of taste.
Whether you use the name "self" or not, you can't refer to ivars in a C function without a qualifier:
@implementation MyClass
- (void)myMethod
{
NSLog(@"%@", _myIvar); // okay because of implicit "self"
}
void myFunction(MyClass *self)
{
NSLog(@"%@", _myIvar); // compile error
NSLog(@"%@", self->_myIvar); // compiles okay
NSLog(@"%@", self.myIvar); // compiles okay if there is a myIvar property
[self myMethod]; // compiles okay
}
@end
--Andy
_______________________________________________
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