我有一个充满问题和答案(带有指定问题)的列表,显示在界面上,但我想随机显示问题。请参阅我在下面尝试的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
if (questions && configDictionary) {
for (int i = 0; i < [questions count]; ++i) { int r = (random() % [questions count]); [questions exchangeObjectAtIndex:i withObjectAtIndex:r]; }
[questionLabel setText:[[questions objectAtIndex:currentQuestonIndex] objectForKey:@"question"]];
NSArray *answers = [[questions objectAtIndex:currentQuestonIndex] objectForKey:@"answers"];
[answerLabel0 setText:[answers objectAtIndex:0]];
[answerLabel1 setText:[answers objectAtIndex:1]];
[answerLabel2 setText:[answers objectAtIndex:2]];
[answerLabel3 setText:[answers objectAtIndex:3]];
[pointsPerAnswerLabel setText:[NSString stringWithFormat:@"+%d points", [[configDictionary objectForKey:kPointsPerCorrectAnswer] intValue]]];
[currentQuestionNumberLabel setText:[NSString stringWithFormat:@"question %d", currentQuestonIndex+1]];
}
}
Someone please help?
我收到警告“NSArray”可能无法响应“exchangeObjectAtIndex:withObjectAtIndex:”然后它在尝试运行后崩溃。
请参阅下面的 .h 文件:
@interface ViewController : UIViewController {
NSDictionary *configDictionary;
NSMutableArray *questions;
int currentQuestonIndex;
NSMutableArray *questionsCorrectlyAnswered;
NSTimer *timer;
int totalTimeLeft;
int currentTimeLeft;
BOOL saveTime;
}
@property (retain, nonatomic) NSMutableArray *questions;
它仍然崩溃。
我以前查看的表格视图:
- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
gameViewController = [[QuizViewController alloc] initWithNibName:@"QuizViewController"bundle:nil];
[(QuizViewController*) gameViewController setMasterViewController:self];
[(QuizViewController*) gameViewController setTitle:[[quizzes objectAtIndex:indexPath.row] objectForKey:@"quizName"]];
[(QuizViewController*) gameViewController setQuestions:[[quizzes objectAtIndex:indexPath.row] objectForKey:@"questions"]];
[gameViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:gameViewController animated:YES];
}