比较对象的索引
Comparing Index At Object
我是 objective C 的新手,我被困在我的任务上。
我已经搜索了问题,但 none 似乎回答了我的特定需求,希望你们能提供帮助! :)
我正在制作一个包含 2 个组件的选择器视图;一个是所有美国,一个是他们的首都。
现在,当按下一个按钮时,我想要一个 'if else statement' 来检查两个数组的索引是否匹配,以及它是否创建了一个动作。
有人可以帮我解决开放条件吗?现在它给了我 'expected expression' 错误。
if (_stateList objectAtIndex:[]== _capitalList objectAtIndex[]) {
// Defining State & capitalnames by row
NSInteger stateRow = [self.picker selectedRowInComponent:StatesList];
NSInteger capitalRow = [self.picker selectedRowInComponent:CapitalsList];
// Defining the Selected states and selected capital in the row
NSString *selectedState = self.stateList[stateRow];
NSString *selectedCapital = self.capitalList[capitalRow];
// Giving a title to the alert message
NSString *title = [[NSString alloc] initWithFormat:@"You selected %@ and %@", selectedState, selectedCapital];
// Alert Message
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:@"Thats right, +1 for you!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Great!" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
// Updating the scores
_rightScore++;
_rightAnswer.text = [NSString stringWithFormat:@"%i", _rightScore];
_scoreTotal = _rightScore - _wrongScore;
_scoreLabel.text = [NSString stringWithFormat:@"Score: %i", _scoreTotal];
} else {
// Defining State & capitalnames by row
NSInteger stateRow = [self.picker selectedRowInComponent:StatesList];
NSInteger capitalRow = [self.picker selectedRowInComponent:CapitalsList];
// Defining the Selected states and selected capital in the row
NSString *selectedState = self.stateList[stateRow];
NSString *selectedCapital = self.capitalList[capitalRow];
// Giving a title to the alert message
NSString *title = [[NSString alloc] initWithFormat:@"You have selected %@ and %@", selectedState, selectedCapital];
// Alert Message
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:@"Thats not correct, -1 for you." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Let me try again!" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
// Updating the scores
_wrongScore++;
_wrongAnswer.text = [NSString stringWithFormat:@"%i", _wrongScore];
_scoreTotal = _rightScore - _wrongScore;
_scoreLabel.text = [NSString stringWithFormat:@"Score: %i", _scoreTotal];
}
您正在将所选行分配给两个变量 stateRow
和 capitalRow
。您似乎想要做的是比较这两个变量以查看它们是否具有相同的值,例如if (stateRow == capitalRow) { ... } else { ... }
HTH
我是 objective C 的新手,我被困在我的任务上。 我已经搜索了问题,但 none 似乎回答了我的特定需求,希望你们能提供帮助! :)
我正在制作一个包含 2 个组件的选择器视图;一个是所有美国,一个是他们的首都。
现在,当按下一个按钮时,我想要一个 'if else statement' 来检查两个数组的索引是否匹配,以及它是否创建了一个动作。 有人可以帮我解决开放条件吗?现在它给了我 'expected expression' 错误。
if (_stateList objectAtIndex:[]== _capitalList objectAtIndex[]) {
// Defining State & capitalnames by row
NSInteger stateRow = [self.picker selectedRowInComponent:StatesList];
NSInteger capitalRow = [self.picker selectedRowInComponent:CapitalsList];
// Defining the Selected states and selected capital in the row
NSString *selectedState = self.stateList[stateRow];
NSString *selectedCapital = self.capitalList[capitalRow];
// Giving a title to the alert message
NSString *title = [[NSString alloc] initWithFormat:@"You selected %@ and %@", selectedState, selectedCapital];
// Alert Message
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:@"Thats right, +1 for you!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Great!" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
// Updating the scores
_rightScore++;
_rightAnswer.text = [NSString stringWithFormat:@"%i", _rightScore];
_scoreTotal = _rightScore - _wrongScore;
_scoreLabel.text = [NSString stringWithFormat:@"Score: %i", _scoreTotal];
} else {
// Defining State & capitalnames by row
NSInteger stateRow = [self.picker selectedRowInComponent:StatesList];
NSInteger capitalRow = [self.picker selectedRowInComponent:CapitalsList];
// Defining the Selected states and selected capital in the row
NSString *selectedState = self.stateList[stateRow];
NSString *selectedCapital = self.capitalList[capitalRow];
// Giving a title to the alert message
NSString *title = [[NSString alloc] initWithFormat:@"You have selected %@ and %@", selectedState, selectedCapital];
// Alert Message
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:@"Thats not correct, -1 for you." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Let me try again!" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
// Updating the scores
_wrongScore++;
_wrongAnswer.text = [NSString stringWithFormat:@"%i", _wrongScore];
_scoreTotal = _rightScore - _wrongScore;
_scoreLabel.text = [NSString stringWithFormat:@"Score: %i", _scoreTotal];
}
您正在将所选行分配给两个变量 stateRow
和 capitalRow
。您似乎想要做的是比较这两个变量以查看它们是否具有相同的值,例如if (stateRow == capitalRow) { ... } else { ... }
HTH