我無法從傳遞的結構中獲取char *字符串。我有以下代碼:麻煩在結構中傳遞C字符串
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通過呢?謝謝!
這解決了這個問題,感謝您的幫助。 – 2013-03-13 17:25:29