• 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: Accessing Cocoa instance variables from C functions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessing Cocoa instance variables from C functions


  • Subject: Re: Accessing Cocoa instance variables from C functions
  • From: William Bumgarner <email@hidden>
  • Date: Mon, 24 Apr 2006 08:19:18 -0700

On Apr 21, 2006, at 1:52 PM, Damien Sorresso wrote:
I've got an Objective-C class that makes use of a C library I've written which has several callbacks. I want to implement these callbacks in my class, but I can't seem to access the class variables from these callbacks. The compiler tells me that the instance variables I'm trying to access are undeclared. Here's a snippet of my code.

void ITPhaseChange(char *message)
{
	NSLog(@"PHASE CHANGE: %s\n", message);
	[phaseText setStringValue:[NSString stringWithCString:message]];
}

Now with just the NSLog(...), everything compiles fine. But I want to work with the `phaseText' outlet from within this function. Is this possible?

Every method of an Objective-C object has two "hidden" arguments that are always present. The first is the all familiar 'self' and the second is '_cmd' -- not relevant here, but interesting anyway.


The 'self' argument effectively provides context to the Obj-C runtime. It is a pointer to the object upon which the method was invoked. It also provides the context via which the compiler can resolve references to instance varaiables.

'phaseText' is an outlet -- an instance variable -- of some class you have designed. As such, you can use it directly in any method that has a 'self' that points to an instance of your class.

Now, obviously, a c function is not a method. It does not have a 'self' -- it is not invoked in the context of a method. It doesn't matter where the C function is declared, this is always the case.

So, to talk to a particular instance of your class -- which is really the question you are asking -- you will need to hold a reference to the instance that contains the 'phaseText' instance variable some place such that you can get to it inside of your C function. If you are talking about 'phaseText' being an instance of a subclass of NSDocument, then it is likely that you'll have more than one 'phaseText' as a possible target -- one per open document.

Some options:

- The most straightforward is if your C API has some kind of a 'context' variable that can be passed in to the API and is used in various callbacks. This will usually be in the form of a (void *). Internally, the code behind the C API does absolutely nothing with the 'context' other than storing it and passing it back to any callback. This is a very standard pattern and you will find it used throughout Mac OS X and many open source projects. Assuming your API has some kind of a "create a connection to me" functionality, then all of this will work transparently with NSDocument.

- If you have a single window app -- a single instance of whatever contains 'phaseText' -- then you can effectively treat your object as a singleton. You can do this through the use of a global variable or thorugh the singleton pattern (search cocoadev for 'singleton' -- google 'cocoadev singleton').

Given that you wrote the C library, I would suggest adding a (void *) context argument to your API. Easy & straightforward. Works regardless of single window vs. document based and your library will remain portable as it does not introduce any Objective-C'isms into the library.

b.bum
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Accessing Cocoa instance variables from C functions
      • From: Scott Ribe <email@hidden>
    • Re: Accessing Cocoa instance variables from C functions
      • From: Ondra Cada <email@hidden>
References: 
 >Accessing Cocoa instance variables from C functions (From: Damien Sorresso <email@hidden>)

  • Prev by Date: Re: Using NSUserDefaultsController with radio buttons
  • Next by Date: Re: Accessing Cocoa instance variables from C functions
  • Previous by thread: Re: Accessing Cocoa instance variables from C functions
  • Next by thread: Re: Accessing Cocoa instance variables from C functions
  • Index(es):
    • Date
    • Thread