我正在尝试阅读plist
,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>question</key>
<string>2 + 2 = ?</string>
<key>A</key>
<string>2</string>
<key>B</key>
<string>4</string>
<key>C</key>
<string>3</string>
</dict>
<dict>
<key>question</key>
<string>2 + 4 = ?</string>
<key>A</key>
<string>3</string>
<key>B</key>
<string>6</string>
<key>C</key>
<string>9</string>
</dict>
</array>
</plist>
我正在尝试在按下UIButton时更新View Controller,但是从数组中读取时似乎有错误。创建一个错误的数组。第一个问题显示正常但按下按钮时会崩溃。断点显示崩溃的行
self.questionasked.text = question [currentQuestion];
这是代码
- (void)viewDidLoad
{
[super viewDidLoad];
rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"]];
currentQuestion = -1;
[self showNextQuestion];
}
-(void) showNextQuestion{
currentQuestion++;
int numItems = [rootArray count];
NSMutableArray *question = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *A = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *B = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *C = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in rootArray) {
[question addObject:[itemData objectForKey:@"question"]];
[A addObject:[itemData objectForKey:@"A"]];
[B addObject:[itemData objectForKey:@"B"]];
[C addObject:[itemData objectForKey:@"C"]];
self.questionasked.text = question[currentQuestion];
self.answer1.text = A[currentQuestion];
self.answer2.text = B[currentQuestion];
self.answer3.text = C[currentQuestion];
}
}
-(IBAction)nextQ:(UIButton *) sender{
[self showNextQuestion];
}
答案 0 :(得分:0)
首先从plist
文件获取数据,此处yourArray
为NSArray
yourArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"plist"]];
for(int i=0; i < yourArray.count; i++)
{
NSLog(@"%@",[array objectAtIndex:i] objectForKey:@"question"]);
NSLog(@"%@",[array objectAtIndex:i] objectForKey:@"A"]);
NSLog(@"%@",[array objectAtIndex:i] objectForKey:@"B"]);
NSLog(@"%@",[array objectAtIndex:i] objectForKey:@"C"]);
}
Consol中的OutPut显示。
答案 1 :(得分:0)
请尝试将这些代码放在for
循环之外。
self.questionasked.text = question[currentQuestion];
self.answer1.text = A[currentQuestion];
self.answer2.text = B[currentQuestion];
self.answer3.text = C[currentQuestion];
答案 2 :(得分:0)
使用时:
rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"]];
你会得到autoreleased
NSArray
,这就是它崩溃的原因,将其更改为:
rootArray = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"]] retain];
如果您使用ARC,请使用:
@property (nonatomic, strong) NSArray *rootArray;
和
self.rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"]];
答案 3 :(得分:0)
首先,你需要改变将plist加载到数组中的方式,因为它会自动释放改变它:
rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"]];
用这个:
rootArray = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"]];
并且你得到另一个会导致崩溃的错误你必须在showNextQuestion方法中获得这些行:
self.questionasked.text = question[currentQuestion];
self.answer1.text = A[currentQuestion];
self.answer2.text = B[currentQuestion];
self.answer3.text = C[currentQuestion];
在for句子中,代码应该是这样的:
-(void) showNextQuestion{
currentQuestion++;
int numItems = [rootArray count];
NSMutableArray *question = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *A = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *B = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *C = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in rootArray) {
[question addObject:[itemData objectForKey:@"question"]];
[A addObject:[itemData objectForKey:@"A"]];
[B addObject:[itemData objectForKey:@"B"]];
[C addObject:[itemData objectForKey:@"C"]];
}
self.questionasked.text = question[currentQuestion];
self.answer1.text = A[currentQuestion];
self.answer2.text = B[currentQuestion];
self.answer3.text = C[currentQuestion];
}
祝你好运..