0

我很难理解这个问题。

我正在创建一个测验。在 plist 中有问题......和可能的答案,这是我的 plist

<plist version="1.0">
<dict>
<key>quest</key>
<array/>
<key>Quest1</key>
<string>3234</string>
<key>A</key>
<string>3</string>
<key>B</key>
<string>2</string>
<key>C</key>
<string>1</string>
</dict>
</plist>

数组被调用quest,我希望能够从代码和 3 个答案中提取“Quest1”......这是我的代码,但我似乎缺少一些东西来让它显示

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [path objectAtIndex:0];
NSString *plistpath = [documentsPath stringByAppendingPathComponent:@"question.plist"];
if(![[NSFileManager defaultManager] fileExistsAtPath:plistpath])
{
    plistpath = [[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistpath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization     propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(@"error reading plist: %@, format:%d",errorDesc, format);

}
  self.question.text = [temp objectForKey:@"quest"];
  self.answer1.text =[temp objectForKey:@"A"];
  self.answer2.text =[temp objectForKey:@"B"];
  self.answer3.text =[temp objectForKey:@"C"];
4

1 回答 1

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"]];
    }
于 2013-04-06T05:04:25.817 回答