2013-02-12 62 views
0

我目前嘗試在iphone上做一個問答遊戲,所以我看到一個教程,但它似乎xcode不明白我的plist或東西:我的標籤是空白,當我運行它: 我.m文件:從plist回覆信息

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

} 

當然我只是把相關的代碼,而不是全部。 有人可以幫忙嗎?

+0

如果你記錄tempDict,你會得到什麼? – rdelmar 2013-02-12 19:26:21

+0

我該怎麼做? (謝謝你的快速回答!) – lea51294 2013-02-12 20:14:27

+0

NSLog(@「%@」,tempDict); – rdelmar 2013-02-12 20:50:34

回答

0

這裏是我的checkForPlist方法,返回一個BOOL如果存在的plist因此可能會值得運行填充一個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; 
    } 
} 

讓我知道這是否有助於!

+0

我必須將.h文件放入什麼位置? (對不起,我確定這很明顯,但我真的剛剛開始) – lea51294 2013-02-13 07:41:33

+0

沒問題 - 您需要在名爲plistDictionary的.h文件中聲明NSDictionary。其他的東西都在這個方法的範圍內聲明,並且在它之外是不必要的。 – TiernanKennedy 2013-02-13 17:21:11

+0

另外我會忽略你的plist文件名中的空格 - 從來不是一個好習慣! – TiernanKennedy 2013-02-13 17:23:56