从plist中检索信息

时间:2013-02-12 19:14:25

标签: ios cocoa-touch plist

我目前正在尝试在iphone上制作一个问答游戏,所以我看到了一个教程,但似乎xcode不理解我的plist或者其他东西:当我运行它时,我的标签是空白的: 在我的.m文件中:

- (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);

}

当然我只是提出了相关的代码,而不是全部。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是我的checkForPlist方法,如果plist存在则返回BOOL并填充NSDictionary,因此值得运行:

-(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;
    }
}

如果有帮助,请告诉我!