从plist中检索数据

时间:2013-02-11 23:28:17

标签: plist

`好的,我试图从我创建的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>

所以我尝试在代码中通过查看各种教程来做到这一点,我只是新手,所以我知道我错了

所以我想把问题放在故事板中创建的标签中,并且与可能的答案相同???? `

- (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"]];`

1 个答案:

答案 0 :(得分: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"]];
        }