Use of iOS incoming C code callback

Source: Internet
Author: User

The situation is the standard use of objective-c calling C and uploading the data back to Objective-c:

Defines a callback function in which Void*context int count is used to pass in the objective object, with the contents of the callback behind it.


So the callback function can be written as

typedef int (*player_callback) (void *opaque, int message, void *data, size_t data_size);


The basic use is as follows:

/* Sqlite_api int sqlite_stdcall sqlite3_exec (
sqlite3*,
const Char *sql,
Int (*callback) (void*,int,char**,char**),
void *,
Char **errmsg
);

*/
static int mycallback (void *context, int count, Char **values, char **columns)
{
Nsmutablearray *names = (__bridge Nsmutablearray *) context;
for (int i=0 i < count; i++) {
const char *namecstring = values[i];
[Names addobject:[nsstring stringwithutf8string:namecstring]];
}
return SQLITE_OK;
}

@implementation Mytableviewcontroller

-(void) loadnamesfromdatabase
{
NSString *file = [[NSBundle mainbundle] pathforresource:@ "names" oftype:@ "DB"];
Sqlite3 *database = NULL;
if (sqlite3_open ([File utf8string], &database) = = SQLITE_OK) {
Sqlite3_exec (Database, "SELECT name from Person", mycallback, names, NULL);
}
Sqlite3_close (database);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.