Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Mixing C and Objective-C methods and function is possible, here is a simple example that uses the SQLite API within an iPhone App: (<a href="http://stanford.edu/class/cs193p/cgi-bin/index.php" rel="nofollow noreferrer">course site</a>)</p> <blockquote> <p>Download the <a href="http://stanford.edu/class/cs193p/cgi-bin/drupal/system/files/sample_code/09_MySQLiteTableView.zip" rel="nofollow noreferrer">Zip file (09_MySQLiteTableView.zip)</a></p> </blockquote> <p>C functions need to be declared outside of the <code>@implementation</code> in an Objective-C (.m) file.</p> <pre><code>int MyCFunction(int num, void *data) { //code here... } @implementation - (void)MyObjectiveCMethod:(int)number withData:(NSData *)data { //code here } @end </code></pre> <p>Because the C function is outside of the <code>@implementation</code> it cannot call methods like </p> <pre><code>[self doSomething] </code></pre> <p>and has no access to ivars.</p> <p>This can be worked around as long as the call-back function takes a <code>userInfo</code> or <code>context</code> type parameter, normally of type <code>void*</code>. This can be used to send any Objective-C object to the C function.</p> <p>As in the <a href="http://stanford.edu/class/cs193p/cgi-bin/drupal/system/files/sample_code/09_MySQLiteTableView.zip" rel="nofollow noreferrer">sample code</a>, this can be manipulated with normal Objective-C operations.</p> <p>In addition please read this answer: <a href="https://stackoverflow.com/questions/801976/mixing-c-functions-in-an-objective-c-class/802059#802059">Mixing C functions in an Objective-C class</a></p>
 

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