Ios 洗牌字典

Ios 洗牌字典,ios,random,nsdictionary,shuffle,Ios,Random,Nsdictionary,Shuffle,我正在尝试制作一个测验应用程序,我希望用户点击一个按钮,程序显示一个随机问题。我尝试使用arc4random,但它确实重复了问题,我想要一个真正的随机方法。所以在这个论坛上,有人告诉我使用洗牌法。我试过: -(IBAction)NextQuestion:(id)sender{ NSDictionary * questions = [[NSDictionary alloc] initWithObjectsAndKeys: @"questiono

我正在尝试制作一个测验应用程序,我希望用户点击一个按钮,程序显示一个随机问题。我尝试使用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)];
}

问题是,不管我问了多少问题,只有一个问题,通常是最后一个,那么我做错了什么?谁能帮帮我吗??谢谢

如果该键只是一个索引,则不要使用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
如果您想从NSDictionary中获得一个随机密钥,那么下面还有一个很好的问题

实例
你当然意识到了

question = [NSMutableArray arrayWithObject:questions];` 
结果只有一个对象的数组-问题字典-所以洗牌数组是无用的

如果你想洗牌,就做吧

NSMutableArray* keys = [[questions allKeys] mutableCopy];
并对其进行排序,然后从该数组中选择要从字典中选择的元素


但进一步观察,由于您初始化ObjectsAndKeys的方式,您将只在字典中输入一个问题,尽管它会在两个不同的键下出现两次。

为什么不创建一个专用的类问题,然后使用该类的对象进行操作呢。使用NSMutableArray而不是NSDictionary。对数组进行洗牌或排序很容易。NSMutableDictionary不能被洗牌或排序,因为它不是一个有序的数据结构。hey zaph的可能重复它不是重复的,我从那篇文章中学到了,你给了我一个有用的答案,但你没有解释清楚,否则你会得到正确的答案,我就不需要再提出新的问题了。谢谢你出色的回答,很可能是正确的答案。当你说创建一个具有属性编号、文本、答案1的问题类。。。你的意思是我应该创建一个新的类并把我所有的问题都放在那里?比如:Question.text=[NSString stringWithFormat:@xxxxxxxxxxxxxxxxxx?],Right2.hidden=NO,error3.hidden=NO,error1.hidden=NO,Answer1.text=[NSString stringWithFormat:@xxxx]。。。这一切?还有,你说的房产号是什么意思?谢谢大家!@Mykod增加了一个例子。如果问题不会改变,您可以使用静态NSArrays初始化问题。如果他们正在改变,最好实现CoreData数据库,问题是NSManagedObject的一个子类。那么你能解释一下我该怎么做吗?我是C新手,从未使用过NSMutableArray甚至NSDictionary,所以如果您足够慷慨的话,我需要更详细的解释。谢谢@Mykod-首先,读取-arrayWithObject,它只向数组中添加一个对象。但是你初始化字典的方法完全错误,而且很难知道从哪里开始。叹气我会用一些信息编辑我的答案。再想想,你的初始化比我想象的还要糟糕。您需要定义一个问题类,或者使每个问题成为一个内部字典,其中包含关键字text、right、error 1、error 2、answer1等。我用我的问题和答案创建了一个.plist,然后添加了我的shuffle和display方法,但有时仍会重复一些问题。我不知道我做错了什么。@Mykod-面对现实吧-你根本不知道你在做什么。从简单的事情开始,确保你理解你在做的每一步。虽然苹果的文档有点糟糕,但还没有那么糟糕,所以在开始使用它们编码之前,先研究一下NSArray和NSDictionary等类的文档。
// 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
question = [NSMutableArray arrayWithObject:questions];` 
NSMutableArray* keys = [[questions allKeys] mutableCopy];