• 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 10:35:36 -0400

On Jul 30, 2013, at 4:19 AM, Rick Mann <email@hidden> wrote:
> On Jul 30, 2013, at 00:59 , Vincent Habchi <email@hidden> wrote:
>
>> I have a very simple question: if I embed a C-function (more precisely, a callback from an external C-library) in an Obj-C object, can I expect this function to behave like a regular method? I.e. can it freely access ‘self’ and other attributes?
>
> No; it'll be a stand-alone method with no implicit knowledge of the Objective-C class.

This may be pedantic, but just to clarify: if you put a C function in MyClass's implementation file you aren't really "embedding" it in the sense that some people might think you mean.  There is no formal association between MyClass and the function, MyClass doesn't know about the function, and you can't call the function as if it were a method.  The function is not a method, stand-alone or otherwise.

The only effect, as others have explained, is on scope; if you put the function inside the @implementation and the function has a reference to an instance of MyClass, then it can use myObj->myIvar for direct access to instance variables.

The same is true for class methods, by the way.  If a class method has a reference to an instance of the class, it can directly access the instance's ivars the same way.

--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


  • Follow-Ups:
    • Re: Mixing Obj-C and C "methods"
      • From: Michael Crawford <email@hidden>
    • Re: Mixing Obj-C and C "methods"
      • From: Scott Ribe <email@hidden>
References: 
 >Mixing Obj-C and C "methods" (From: Vincent Habchi <email@hidden>)
 >Re: Mixing Obj-C and C "methods" (From: Rick Mann <email@hidden>)

  • Prev by Date: Re: Exiting non-POSIX threads?
  • 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