-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)];
}

問題は、私が広告を出した質問の数に関係なく、表示されるのは 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 からランダムなキーが必要な場合は、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 に答える