我无法从传递的结构中获取 char* 字符串。我有以下代码:
typedef struct {
NSInteger id;
char *title;
} Movie;
...
Movie movie = [self randomMovie];
NSInteger movieID = movie.id;
NSString *movieTitle = [NSString stringWithUTF8String:movie.title];
NSLog(@"movieTitle: %@", movieTitle);
...
- (Movie)randomMovie {
sqlite3_stmt *statement;
NSString *query = @"SELECT id, title FROM movies ORDER BY RANDOM() LIMIT 1;";
Movie movie;
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_ROW) {
// Get the id and title of the first
movie.id = sqlite3_column_int(statement, 0);
movie.title = (char *)sqlite3_column_text(statement, 1);
}
}
NSLog(@"Random movie %d title: %@", movie.id, [NSString stringWithUTF8String:movie.title]);
sqlite3_finalize(statement);
return movie;
}
这给了我输出:
2013-03-13 10:10:39.438 Fabflix[89156:c07] Random movie 872011 title: Ray
2013-03-13 10:10:39.439 Fabflix[89156:c07] movieTitle: (null)
有谁知道为什么标题字符串没有从 randomMovie 正确传递?谢谢!