如果键只是一个索引,不要使用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