iOS でクイズ ゲームを進めようとしていますが、問題があります。sqlite配列とランダムな質問と回答を正常に作成しましたが、回答メソッドを作成する方法がわかりません。すべての質問の sqlite テーブルでは、answer1 列が正解です。では、ボタンで正解をチェックするメソッドを作成するにはどうすればよいでしょうか。上記は、ランダムな質問とランダムな回答のコードです。
-(NSMutableArray *) categoriesList{
categories = [[NSMutableArray alloc] initWithCapacity:10];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Categories.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
}
const char *sql = "SELECT * FROM Questions WHERE Category IS 2 ORDER BY RANDOM() LIMIT 1";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
Categories * choise = [[Categories alloc] init];
question.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 1)];
NSMutableArray *arary = [[NSMutableArray alloc]init];
while (arary.count < 4) {
int value = arc4random()%4+2;
BOOL isFound = [[arary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"intValue == %d",value]]] count];
if(!isFound)
[arary addObject:[NSNumber numberWithInteger:value]];
}
answer1.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:0] intValue])];
answer2.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:1] intValue])];
answer3.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:2] intValue])];
answer4.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:3] intValue])];
[categories addObject:choise];
}
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
@finally {
sqlite3_close(db);
return categories;
}
そして、これが答えの方法を作成しようとする私の試みです。
- (IBAction)answer1:(id)sender {
if([answer1.text isEqualToString:@"2"])
{
NSLog(@"Correct");
}else{
NSLog(@"Incorrect");
}
}
- (IBAction)answer2:(id)sender {
if([answer2.text isEqualToString:@"2"])
{
NSLog(@"Correct");
}else{
NSLog(@"Incorrect");
}
}
- (IBAction)answer3:(id)sender {
if([answer3.text isEqualToString:@"2"])
{
NSLog(@"Correct");
}else{
NSLog(@"Incorrect");
}
}
- (IBAction)answer4:(id)sender {
if([answer4.text isEqualToString:@"2"])
{
NSLog(@"Correct");
}else{
NSLog(@"Incorrect");
}
}
4つのボタンで正解方法を作成する方法を誰か助けてもらえますか?