Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

How can I Shuffle my plist?

Hello, friends. I am new here, so I will introduce myself. My name is Plamen and I am from Bulgaria. I need some help about my quiz application. I can't shuffle the questions and answers from my plist. Here's my code - I have 1 label (for the question) and 4 buttons (for the answers), please tell me how to shuffle the plist:
THE .H FILE:

#import


@interface SecondViewController : UIViewController {
int mainInt;
IBOutlet UILabel *timer;
NSTimer *randomMain;
//that was for the timer
int currentQuestion;
}
@property (retain, nonatomic) IBOutlet UILabel *timer; //this is for the timer
@property (retain, nonatomic) IBOutlet UILabel *textQuestionTitle;
@property (retain, nonatomic) IBOutlet UIButton *buttonA;
@property (retain, nonatomic) IBOutlet UIButton *buttonB;
@property (retain, nonatomic) IBOutlet UIButton *buttonC;
@property (retain, nonatomic) IBOutlet UIButton *buttonD;
@property (retain, nonatomic) NSArray *questions;
@property (retain, nonatomic) NSString *answer;

-(void)showNextQuestion;
@end
================================================================

THE .M FILE:

#import "SecondViewController.h"

@implementation SecondViewController
@synthesize timer, buttonA, buttonB, buttonC, buttonD, textQuestionTitle;
@synthesize questions = questions_;
@synthesize answer = answer_;

-(void)randomMainVoid {
mainInt -= 1;
timer.text = [NSString stringWithFormat:@"Time left: %d", mainInt];
if (mainInt == 0) {
[randomMain invalidate];
randomMain = nil;
}
}


- (void)viewDidLoad {
[super viewDidLoad];
mainInt = 11;
randomMain = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"];
self.questions = [NSArray arrayWithContentsOfFile:path];
currentQuestion = -1;
[self showNextQuestion];
}


-(void)showNextQuestion {
currentQuestion ++;
if (currentQuestion < [self.questions count]) {
NSDictionary *nextQuestion = [self.questions objectAtIndex:currentQuestion];
self.answer = [nextQuestion objectForKey:@"QuestionAnswer"];
self.textQuestionTitle = [nextQuestion objectForKey:@"QuestionTitle"];
NSString *varA = [nextQuestion objectForKey:@"A"];
[buttonA setTitle: varA forState:UIControlStateNormal];
NSString *varB = [nextQuestion objectForKey:@"B"];
[buttonB setTitle: varB forState:UIControlStateNormal];
NSString *varC = [nextQuestion objectForKey:@"C"];
[buttonC setTitle: varC forState:UIControlStateNormal];
NSString *varD = [nextQuestion objectForKey:@"D"];
[buttonD setTitle: varD forState:UIControlStateNormal];
}
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}

@end
Here's my plist:
image

P.S: Sorry for my bad english.
Post edited by scourgy on

Replies

  • spencerharry80spencerharry80 Posts: 9New Users
    I am also looking for solution, is there any particular code available for that?
  • Duncan CDuncan C Posts: 8,031Tutorial Authors, Registered Users
    scourgy said:

    Hello, friends. I am new here, so I will introduce myself. My name is Plamen and I am from Bulgaria. I need some help about my quiz application. I can't shuffle the questions and answers from my plist. Here's my code - I have 1 label (for the question) and 4 buttons (for the answers), please tell me how to shuffle the plist:

    THE .H FILE:

    #import


    @interface SecondViewController : UIViewController {
    int mainInt;
    IBOutlet UILabel *timer;
    NSTimer *randomMain;
    //that was for the timer
    int currentQuestion;
    }
    @property (retain, nonatomic) IBOutlet UILabel *timer; //this is for the timer
    @property (retain, nonatomic) IBOutlet UILabel *textQuestionTitle;
    @property (retain, nonatomic) IBOutlet UIButton *buttonA;
    @property (retain, nonatomic) IBOutlet UIButton *buttonB;
    @property (retain, nonatomic) IBOutlet UIButton *buttonC;
    @property (retain, nonatomic) IBOutlet UIButton *buttonD;
    @property (retain, nonatomic) NSArray *questions;
    @property (retain, nonatomic) NSString *answer;

    -(void)showNextQuestion;
    @end
    ================================================================

    THE .M FILE:

    #import "SecondViewController.h"

    @implementation SecondViewController
    @synthesize timer, buttonA, buttonB, buttonC, buttonD, textQuestionTitle;
    @synthesize questions = questions_;
    @synthesize answer = answer_;

    -(void)randomMainVoid {
    mainInt -= 1;
    timer.text = [NSString stringWithFormat:@"Time left: %d", mainInt];
    if (mainInt == 0) {
    [randomMain invalidate];
    randomMain = nil;
    }
    }


    - (void)viewDidLoad {
    [super viewDidLoad];
    mainInt = 11;
    randomMain = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"];
    self.questions = [NSArray arrayWithContentsOfFile:path];
    currentQuestion = -1;
    [self showNextQuestion];
    }


    -(void)showNextQuestion {
    currentQuestion ++;
    if (currentQuestion < [self.questions count]) {
    NSDictionary *nextQuestion = [self.questions objectAtIndex:currentQuestion];
    self.answer = [nextQuestion objectForKey:@"QuestionAnswer"];
    self.textQuestionTitle = [nextQuestion objectForKey:@"QuestionTitle"];
    NSString *varA = [nextQuestion objectForKey:@"A"];
    [buttonA setTitle: varA forState:UIControlStateNormal];
    NSString *varB = [nextQuestion objectForKey:@"B"];
    [buttonB setTitle: varB forState:UIControlStateNormal];
    NSString *varC = [nextQuestion objectForKey:@"C"];
    [buttonC setTitle: varC forState:UIControlStateNormal];
    NSString *varD = [nextQuestion objectForKey:@"D"];
    [buttonD setTitle: varD forState:UIControlStateNormal];
    }
    }

    - (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
    }

    - (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    }
    - (void)dealloc {
    [super dealloc];
    }

    @end
    Here's my plist:
    image

    P.S: Sorry for my bad english.
    Instead of having 1 property questions, create an NSArray property allQuestions and an NSMutableArray property remainingQuestions:
    @property (retain, nonatomic) NSArray *allQuestions;
    @property (retain, nonatomic) NSMutableArray *remainingQuestions;

    Change this line:
    self.allQuestions = [NSArray arrayWithContentsOfFile:path];
    Then to populate your list of remaining questions:
    self.remainingQuestions = [[self.allQuestions mutableCopy] autorelease];
    And to fetch a question:

    if (remainingQuestions.count == 0)
    self.remainingQuestions = [[self.allQuestions mutableCopy] autorelease];

    NSInteger index = arc4random_uniform([self.remainingQuestions count]);
    NSDictionary *nextQuestion = [remainingQuestions objectAtIndex: index];
    [remainingQuestions removeObjectAtIndex: index];
    The code above fetches questions in random order, removes each question from the list of current questions once it's been fetched, and then repopulates the array after all the questions have been fetched.

    Disclaimer: I typed this code into a browser window have not compiled it, much less tested it. It is perfectly likely to contain a few syntax errors, and may even contain minor bugs.
    Post edited by Duncan C on
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.