2013-02-11 167 views
0

`好了,所以我想拉的問題和可能的答案由我創建從plist中檢索數據

<key>Question1</key> 
<string></string> 
<key>Answer1</key> 
<array/> 
<key>4</key> 
<string>A</string> 
<key>2</key> 
<string>B</string> 
<key>3</key> 
<string>C</string> 

所以我試圖做到這一點的代碼看起來各種教程的plist文件,我我只是新來的,所以我知道我有這一切錯誤

所以,然後我想把問題放在故事板中創建的標籤和相同的可能的答案? `

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

NSString *listPath = [[self docDir]stringByAppendingPathComponent:@"work.plist"]; 
if (![[NSFileManager defaultManager]fileExistsAtPath:listPath]) { 
[[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"work" ofType:@"plist"]toPath:listPath error:nil]; 

} 
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:listPath];NSLog(@"count: %i", [array count]); 

NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization 
           propertyListFromData:@"work.plist"; 
self.firstQuestion = [temp objectForKey:@"Question1"]; 
self.firstanswer = [NSMutableArray arrayWithArray:[temp objectForKey:@"Answer"]];` 

回答

0

也試過這個。

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [path objectAtIndex:0]; 
    NSString *plistpath = [documentsPath stringByAppendingPathComponent:@"question.plist"]; 
    NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile:plistFilePath]; 
    for (int i=0; i<[savedStock count]; i++) 
     { 
      [A-array addObject:[[savedStock objectAtIndex:i] valueForKey:@"A"]]; 
      [B-array addObject:[[savedStock objectAtIndex:i] valueForKey:@"B"]]; 
     }