Note that there are some explanatory texts on larger screens.

plurals
  1. POivars when mixing C and Objective-C
    primarykey
    data
    text
    <p>I'm writing an Objective-C wrapper for a C library and having problems accessing the references of my ivars.</p> <p>The C library requires that I specify a function pointer that handles events, I specify it during the initialization of my Objective-C class.</p> <pre> - (id) init { [super init]; RegisterClient(&handleEvent); return self; } </pre> <p>The C library is able to start searching for something and will then call the handleEvent function in case anything happened during the search. The function (basically) looks like this.</p> <pre> int handleEvent(void *Event) { [delegate didFinishSearching]; return 0; } </pre> <p>At least I'd wish it looked like this. The problem is that the code won't build because <code>'delegate' undeclared (first use in function)</code> (of course I have declared it, I can call <code>[delegate didFinishSearching]</code> from any Objective-C method but not from a C function). Older questions from stackoverflow suggest to define an additional variable (e.g. <code>theDelegate</code>) in the header file:</p> <pre> id theDelegate; @interface Controller : NSObject { id delegate; } @property (assign) id delegate; @end </pre> <p>Then, whenever I change the value of <code>delegate</code> to a new one, I have to assign this value to <code>theDelegate</code>, too.</p> <p>Since my C is somewhat rusty, here are my questions:</p> <p>1) Can I pass the <code>RegisterClient</code> C function a pointer to an Objective-C method instead of a pointer to a function as an argument in order to avoid the C function <code>handleEvent</code> completely?</p> <p>2) If not: When I create multiple instances of this Objective-C class, will <code>theDelegate</code> be the same for all instances? (After all, it's not declared as an instance variable...)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload