Search code examples
ioscocoa-touchplist

Retrieve information from plist


I'm currently trying to make a quiz game on iphone so I saw a tutorial but it seems xcode doesn't understand my plist or something: my labels are blank when I run it: in my .m file:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

questionNumber = 0;
NSString * path = [[NSBundle mainBundle] pathForResource:@"Propriety List" ofType:@"plist"];
if (path) {
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile: path];
self.Question = [tempDict objectForKey:@"Root"];}
currentQuestion = -1;
}


-(void) showNextQuestion {
currentQuestion++;
if (currentQuestion < [self.Question count]) {
    NSDictionary *nextquestion = [self.Question objectAtIndex:currentQuestion];
    self.Answear = [nextquestion objectForKey:@"QuestionAnswear"];
    self.labelQuestionTitle.text = [nextquestion objectForKey:@"QuestionTitle"];
    self.labelAnswearA.text = [nextquestion objectForKey:@"A"];
    self.labelAnswearB.text = [nextquestion objectForKey:@"B"];
    self.labelAnswearC.text = [nextquestion objectForKey:@"C"];
    self.labelAnswearD.text = [nextquestion objectForKey:@"D"];
    self.labelAnswearE.text = [nextquestion objectForKey:@"E"];
    }
else {
    // Game over
    }
}


- (IBAction)buttonPressedA:(id)sender{
if ([self.Answear isEqualToString:@"A"]) {
    numCorrect++;
    NSLog(@"%d", numCorrect);

}

Of course I've just put on the related codes and not all of them. Can someone help please??


Solution

  • Here is my checkForPlist Method, returns a BOOL and fills an NSDictionary if the plist exists so could be worth running:

    -(BOOL) checkForPlist
    {
        NSString *errorDesc = nil;
        NSPropertyListFormat format;
        NSString *plistPath;
        NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                  NSUserDomainMask, YES) objectAtIndex:0];
        plistPath = [rootPath stringByAppendingPathComponent:@"TestPlist.plist"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
            plistPath = [[NSBundle mainBundle] pathForResource:@"TestPlist" ofType:@"plist"];
        }
        NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
        self.plistDictionary = (NSMutableDictionary *)[NSPropertyListSerialization
                                               propertyListFromData:plistXML
                                               mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                               format:&format
                                               errorDescription:&errorDesc];
        if (!self.plistDictionary) {
            NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
            return NO;
        }
        else
        {
            NSLog(@"Plist exists - %@", [self.plistDictionary objectForKey:@"LastUpdated"]);
            return YES;
        }
    }
    

    Let me know if that helps!