-4

我正在尝试制作一个测验应用程序,我希望用户单击一个按钮并且程序显示一个随机问题。我尝试使用 arc4random,但它确实重复了这些问题,我想要一个真正随机的方法。所以在这个论坛上有人告诉我使用洗牌方法。我试过了 :

-(IBAction)NextQuestion:(id)sender{

NSDictionary * questions = [[NSDictionary alloc] initWithObjectsAndKeys:
                          @"questionone", Question.text = [NSString stringWithFormat:@"xxxxxxxxxxxxxxxx?"],
                           Right2.hidden = NO,
                           Wrong3.hidden = NO,
                           Wrong1.hidden = NO,
                           Answer1.text = [NSString stringWithFormat:@"xxxx"],
                           Answer2.text = [NSString stringWithFormat:@"xxxx"],
                           Answer3.text = [NSString stringWithFormat:@"xxxx"],
                           @"questiontwo", Question.text = [NSString stringWithFormat:@"xxxxxxxxxxxxxxxxxxxx?"],
                           Right1.hidden = NO,
                           Wrong2hidden = NO,
                           Wrong3.hidden = NO,
                           Answer1.text = [NSString stringWithFormat:@"xxxx"],
                           Answer2.text = [NSString stringWithFormat:@"xxxx"],
                           Answer3.text = [NSString stringWithFormat:@"xxxx"],
                            nil];

NSMutableArray *question;
question = [NSMutableArray arrayWithObject:questions];



// shuffle
for (int i = (int)[question count] - 1; i > 0; --i) {
    [question exchangeObjectAtIndex: i
                  withObjectAtIndex: arc4random_uniform((uint32_t)i + 1)];
}

问题是,无论我提出多少问题,只有一个节目(通常是最后一个),所以我做错了什么?有人可以帮我吗?谢谢!!

4

2 回答 2

4

如果键只是一个索引,不要使用NSDictionary. 默认情况下不订购它们!

// create a Question class with the properties number,text, answer1, ... etc. (need .h/.m and import it)

unsigned int maxCountOfQuestions = ...
NSMutableArray *tempArray = [NSMutableArray alloc] initWithCapacity: maxCountOfQuestions];
for (unsigned int index=0; index < maxCountOfQuestions; i++) {
    // make an designated initializer for your `Question` object
    Question *question = [[Question alloc] initWithText:@"textone" number:2 ...];
    [tempArray addObject:question];
}
NSArray *questions = [NSArray arrayFromArray:tempArray];
[questions shuffle]; // don't forget to import the category for the shuffle method

你想要一个NSArray,洗牌可以用这个类别来完成:

//  NSMutableArray_Shuffling.h

#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#include <Cocoa/Cocoa.h>
#endif

// This category enhances NSMutableArray by providing
// methods to randomly shuffle the elements.
@interface NSMutableArray (Shuffling)
- (void)shuffle;
@end


//  NSMutableArray_Shuffling.m

#import "NSMutableArray_Shuffling.h"

@implementation NSMutableArray (Shuffling)

- (void)shuffle
{
    NSUInteger count = [self count];
    for (NSUInteger i = 0; i < count; ++i) {
        // Select a random element between i and end of array to swap with.
        NSInteger nElements = count - i;
        NSInteger n = (arc4random() % nElements) + i;
        [self exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
}

@end

如果你想要一个来自 NSDictionary 的随机密钥,在How do I select a random key from an NSDictionary?中还有一个关于它的好问题?


例子

// main

unsigned int maxCountOfQuestions = 40u;
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:maxCountOfQuestions];
Question *question1 = [[Question alloc] initWithTxt:@"Color of Apples?" Answer:@"yellow" Answer:@"green" Answer:@"blue" Number:1];
[tempArray addObject:question1];

Question *question2 = [[Question alloc] initWithTxt:@"Color of Bananas?" Answer:@"yellow" Answer:@"green" Answer:@"blue" Number:2];
[tempArray addObject:question2];

Question *question3 = [[Question alloc] initWithTxt:@"Color of Peaches?" Answer:@"yellow" Answer:@"green" Answer:@"blue" Number:3];
[tempArray addObject:question3];

Question *question4 = [[Question alloc] initWithTxt:@"Color of Oranges?" Answer:@"yellow" Answer:@"green" Answer:@"blue" Number:4];
[tempArray addObject:question4];

NSArray *questions = [NSArray arrayWithArray:tempArray];
[questions shuffle]; // don't forget to import the category for the shuffle method

// you can change the questions with changing only one property
questions[3].answer1 = @"purple";

// Questions.h
#import <Foundation/Foundation.h>

@interface Question : NSObject

@property (nonatomic, readwrite, strong) NSString *questionText;
@property (nonatomic, readwrite, strong) NSString *answer1;
@property (nonatomic, readwrite, strong) NSString *answer2;
@property (nonatomic, readwrite, strong) NSString *answer3;
@property (nonatomic, readwrite, unsafe_unretained) unsigned int number;

- (id)initWithTxt:(NSString *)txt Answer:(NSString *)a1 Answer:(NSString *)a2 Answer:(NSString *)a3 Number:(unsigned int)num;

@end

// Questions.m
#import "Question.h"

@implementation Question

@synthesize questionText = _questionText;
@synthesize answer1 = _answer1;
@synthesize answer2 = _answer2;
@synthesize answer3 = _answer3;
@synthesize number = _number;

//designated initializer
- (id)initWithTxt:(NSString *)txt Answer:(NSString *)a1 Answer:(NSString *)a2 Answer:(NSString *)a3 Number:(unsigned int)num
{
    self = [super init];
    if (self != nil) {
        _questionText = txt;
        _answer1 = a1;
        _answer2 = a2;
        _answer3 = a3;
        _number = num;
    }

    return self;
}

@end
于 2013-11-10T12:04:21.210 回答
4

你当然意识到

question = [NSMutableArray arrayWithObject:questions];` 

导致只有一个对象的数组 -questions字典 - 所以“洗牌”数组是无​​用的。

如果你想洗牌,做

NSMutableArray* keys = [[questions allKeys] mutableCopy];

并对其进行排序,然后从该数组中选择元素以从字典中进行选择。

(但进一步观察,由于您执行 initObjectsAndKeys 的方式,您只会将一个问题放入您的字典中,尽管它会在两个不同的键下出现两次。)

于 2013-11-10T13:36:46.127 回答