• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Mixing Obj-C and C "methods"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Mixing Obj-C and C "methods" (From: Vincent Habchi <email@hidden>)
 >Re: Mixing Obj-C and C "methods" (From: Michael Crawford <email@hidden>)

  • Prev by Date: Re: Mixing Obj-C and C "methods"
  • Next by Date: Re: Mixing Obj-C and C "methods"
  • Previous by thread: Re: Mixing Obj-C and C "methods"
  • Next by thread: Re: Mixing Obj-C and C "methods"
  • Index(es):
    • Date
    • Thread