Ios 如何在使用“时增加索引”;objectAtindex“;用于数组中的词典
所以我们有一个plist,它有一系列字典。该数组是allCategories,目前有两个字典充当问题。每本字典都有一组字符串。目标是,当您按下按钮时,它将检查它是否正确,如果正确,则在allCategory数组中为新词典增加currentQuestion、setTitle和picture。然而,这不起作用,我们不知道如何修复它。到目前为止,它认识到这是一个正确的问题,它应该改变它,但没有显示任何内容。如果我们使currentQuestion=1并删除currentQuestion++并单击按钮,它将正常工作。我们希望它根据它是否正确而递增Ios 如何在使用“时增加索引”;objectAtindex“;用于数组中的词典,ios,objective-c,arrays,Ios,Objective C,Arrays,所以我们有一个plist,它有一系列字典。该数组是allCategories,目前有两个字典充当问题。每本字典都有一组字符串。目标是,当您按下按钮时,它将检查它是否正确,如果正确,则在allCategory数组中为新词典增加currentQuestion、setTitle和picture。然而,这不起作用,我们不知道如何修复它。到目前为止,它认识到这是一个正确的问题,它应该改变它,但没有显示任何内容。如果我们使currentQuestion=1并删除currentQuestion++并单击按钮,
- (IBAction)showPic
{
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"]]; //sets the proper plist file
allCategory = [picturesDictionary objectForKey:@"AllCategory"]; //Sets the array allCategory
currentQuestion++; //There are currently 2 dictionaries acting as the questions.
NSDictionary *QuestionNumber = [allCategory objectAtIndex:currentQuestion]; answerKey = [QuestionNumber objectForKey:@"correctAnswer"];
correctAnswer = [QuestionNumber objectForKey:answerKey]; //the two lines above determine what the correct answer is based on the plist.
if([self.buttonOutlet.currentTitle isEqualToString:correctAnswer]) //this is where the comparison is made.
{
NSLog(@"currentQuestion:%d", currentQuestion);
//the button titles should change to the next dictionary in allCategory, however it wont change.
self.Label.text = @"This is working";
}
else if(currentQuestion != 0){
[self.buttonOutlet setTitle: [QuestionNumber objectForKey:@"A"] forState:UIControlStateNormal];
[self.buttonTwo setTitle: [QuestionNumber objectForKey:@"B"] forState:UIControlStateNormal];
[self.buttonThree setTitle: [QuestionNumber objectForKey:@"C"] forState:UIControlStateNormal];
[self.buttonFour setTitle: [QuestionNumber objectForKey:@"D"] forState:UIControlStateNormal];
UIImage *img = [UIImage imageNamed: [QuestionNumber objectForKey:@"fileName"]];
[imageHolder setImage:img];
self.Label.text = @"This is correct";
}
else {
self.Label.text = @"This is not correct";
}
}
这就是普利斯特
<?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">
<dict>
<key>AllCategory</key>
<array>
<dict>
<key>fileName</key>
<string>gta.jpg</string>
<key>A</key>
<string>gta</string>
<key>B</key>
<string>COD</string>
<key>C</key>
<string>LOL</string>
<key>D</key>
<string>Watchdogs</string>
<key>correctAnswer</key>
<string>A</string>
</dict>
<dict>
<key>fileName</key>
<string>LOL.jpg</string>
<key>A</key>
<string>Watchdogs</string>
<key>B</key>
<string>La Noir</string>
<key>C</key>
<string>Dota 2</string>
<key>D</key>
<string>fifa</string>
<key>correctAnswer</key>
<string>D</string>
</dict>
</array>
</dict>
</plist>
所有类别
文件名
gta.jpg
A.
gta
B
货到付款
C
英雄联盟
D
看门狗
正确答案
A.
文件名
LOL.jpg
A.
看门狗
B
黑色
C
Dota 2
D
国际足联
正确答案
D
您需要删除
int currentQuestion=0代码>
并将其设置在iAction
之外的某个位置,否则每次按下按钮,它都会将currentQuestion
重置为0
为什么要为答案创建一个键
answerKey = [QuestionNumber objectForKey:@"correctAnswer"];
correctAnswer = [QuestionNumber objectForKey:answerKey];
你应该可以直接访问它
correctAnswer = [QuestionNumber objectForKey:@"correctAnswer"];
摆脱回答键,这实际上是一个可变范围的问题。在.h文件中定义的属性对类中的所有方法全局可见,并且对子类化或使用类的任何外部类都可见。这些是可见的公共属性,以便将来可能重复使用您的方法的其他人可以将信息传递到您的方法中,而不必知道内部功能。您还可以在.m文件的顶部有一个接口部分。这适用于像.h文件这样的方法,这些方法在类中具有全局作用域,但它们对使用您的类的其他类不可见。这两种类型的属性都是通过使用self在方法中访问的。前缀在方法内部定义的变量仅在该方法内部可见,不需要self。前缀局部变量具有其所在方法的生命周期。每次重新输入该方法时,都会创建该变量的新实例
在您的例子中,currentQuestion被定义为方法中的局部变量。每次进入时,都会创建变量并将其初始化为零。按照meda所说的做并将其移到方法之外,它是一个全局属性,可以被类中的所有方法看到。在viewDidLoad或ViewController生命早期初始化的其他方法中初始化它时,将其设置为零一次,然后像在方法的if语句中一样递增
将它移到if语句的底部不会有任何帮助,因为您似乎在if语句中的任何地方都没有引用它(除非我遗漏了一个),但是将它移到底部确实消除了关于if语句中显示哪些内容的任何混淆。因此,从这个角度来看,这很好
试试这个:
#import "exViewController.h"
@interface exViewController ()
@property (nonatomic) NSInteger currentQuestion;
@property (weak, nonatomic) IBOutlet UILabel *Label;
@property (weak, nonatomic) IBOutlet UIImageView *imageHolder;
@property (weak, nonatomic) IBOutlet UIButton *buttonOutlet;
@property (weak, nonatomic) IBOutlet UIButton *buttonTwo;
@property (weak, nonatomic) IBOutlet UIButton *buttonThree;
@property (weak, nonatomic) IBOutlet UIButton *buttonFour;
@property (nonatomic,strong) NSDictionary *picturesDictionary;
@property (nonatomic,strong) NSArray *allCategory;
@property (nonatomic,strong) NSDictionary *QuestionNumber;
@end
@implementation exViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// we have to initialize the screen with the first question.
// There isn't any reason to re-initialize these every time you pick an answer do
// do it up here in viewDidLoad
self.picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"PropertyList" ofType:@"plist"]]; //sets the proper plist file
self.allCategory = [self.picturesDictionary objectForKey:@"AllCategory"]; //Sets the array allCategory
// Initialize currentQuestion to 0
self.currentQuestion=0;
// I put the statements to display the question into another method so they wouldn't
// have to be put in the code multiple times. Makes it easier to maintain.
[self displayQuestion:self.currentQuestion];
}
// display question takes one arguement, the value for currentQuestion so it's going
// to display the question associated with whatever index is.
- (void) displayQuestion:(NSInteger) index
{
// get questionNumber like you did before, but using index which we passed into this method.
self.QuestionNumber = [self.allCategory objectAtIndex:index];
self.picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"PropertyList" ofType:@"plist"]]; //sets the proper plist file
self.allCategory = [self.picturesDictionary objectForKey:@"AllCategory"]; //Sets the array allCategory
[self.buttonOutlet setTitle: [self.QuestionNumber objectForKey:@"A"] forState:UIControlStateNormal];
[self.buttonTwo setTitle: [self.QuestionNumber objectForKey:@"B"] forState:UIControlStateNormal];
[self.buttonThree setTitle: [self.QuestionNumber objectForKey:@"C"] forState:UIControlStateNormal];
[self.buttonFour setTitle: [self.QuestionNumber objectForKey:@"D"] forState:UIControlStateNormal];
UIImage *img = [UIImage imageNamed: [self.QuestionNumber objectForKey:@"fileName"]];
[self.imageHolder setImage:img];
}
//This was your old showPic method. I added the sender parameter being sent to it.
// it points to the button that was clicked.
- (IBAction)showPicture:(UIButton *)sender
{
NSDictionary *QuestionNumber = [self.allCategory objectAtIndex:self.currentQuestion];
NSString *answerKey = [QuestionNumber objectForKey:@"correctAnswer"];
NSString *correctAnswer = [QuestionNumber objectForKey:answerKey]; //the two lines above determine what the correct answer is based on the plist.
NSLog(@"answerkey=%@",answerKey);
NSLog(@"Correctanswer=%@",correctAnswer);
NSLog(@"sender=%@",sender.description);
//remember we are now passing in the address of the button that we clicked so we
//can just checking sender(the button).titleLabel.text and see if it's equalto the correct answer.
if(![sender.titleLabel.text isEqualToString:correctAnswer])
{
// wrong answer
NSLog(@"currentQuestion:%d", self.currentQuestion);
self.Label.text = @"Wrong Answer";
}
else {
// right answer go to next question
self.currentQuestion++;
// make sure we don't go outside our bounds past the last question.
if(self.currentQuestion<[self.allCategory count]) {
self.Label.text = @"This is correct!! Next question";
// call the method to display the question using the incremented currentQuestion.
[self displayQuestion:self.currentQuestion];
} else {
self.Label.text = @"Correct answer end of test";
}
}
}
@end
#导入“exViewController.h”
@接口exViewController()
@性质(非原子)NSInteger问题;
@属性(弱,非原子)IBUILabel*标签;
@属性(弱、非原子)iBuiImageView*imageHolder;
@属性(弱,非原子)IBUIButton*buttonOutlet;
@属性(弱,非原子)IBUIButton*buttonTwo;
@属性(弱,非原子)IBUIButton*ButtontTree;
@属性(弱,非原子)IBUIButton*buttonFour;
@属性(非原子,强)NSDictionary*picturesDictionary;
@属性(非原子,强)NSArray*所有类别;
@属性(非原子,强)NSDictionary*QuestionNumber;
@结束
@exViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
//我们必须用第一个问题初始化屏幕。
//没有任何理由在每次选择答案时都重新初始化它们
//在viewDidLoad中在这里执行
self.picturesDictionary=[NSDictionary Dictionary WithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“plist”类型的“PropertyList”];//设置正确的plist文件
self.allCategory=[self.picturesDictionary objectForKey:@“allCategory”];//设置数组allCategory
//将currentQuestion初始化为0
self.currentQuestion=0;
//我将这些语句放在另一个方法中以显示问题,这样它们就不会
//必须多次放入代码中。这样更易于维护。
[自我显示问题:self.currentQuestion];
}
//显示问题接受一个参数,即currentQuestion的值
//显示与任何索引关联的问题。
-(无效)显示问题:(NSInteger)索引
{
//像以前一样获取questionNumber,但使用我们传递给此方法的索引。
self.QuestionNumber=[self.allCategory objectAtIndex:index];
self.picturesDictionary=[NSDictionary Dictionary WithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“plist”类型的“PropertyList”];//设置正确的plist文件
self.allCategory=[self.picturesDictionary objectForKey:@“allCategory”];//设置数组allCategory
[self.buttonOutlet setTitle:[self.QuestionNumber objectForKey:@“A”]用于状态:UIControlStateNormal];
[self.button两个设置标题:[self.QuestionNumber objectForKey:@“B”]用于状态:UIControlStateNormal];
[self.buttonThree setTitle:[self.QuestionNumber objectForKey:@“C”]用于状态:UIControlSta