2013-02-16 99 views
0

我在解決這個問題時遇到了很大的困難。從plist數組中檢索數據

我正在創建一個測驗。在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"]; 

回答

0

試試下面code..it將幫助您

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"]]; 
    }