Objective c 在对象上比较索引
我是C目标的新手,我一直在努力。 我搜索了一些问题,但似乎没有一个能回答我的具体需求,希望你们能帮助我!:) 我正在制作一个包含2个组件的选择器视图;一个是全美国,一个是首都 现在,当按下一个按钮时,我需要一个“if-else语句”来检查两个数组的索引是否匹配,以及它是否创建了一个操作。 谁能帮我弄一下开张的条件吗?现在它给了我“预期表达式”错误Objective c 在对象上比较索引,objective-c,arrays,indexing,picker,Objective C,Arrays,Indexing,Picker,我是C目标的新手,我一直在努力。 我搜索了一些问题,但似乎没有一个能回答我的具体需求,希望你们能帮助我!:) 我正在制作一个包含2个组件的选择器视图;一个是全美国,一个是首都 现在,当按下一个按钮时,我需要一个“if-else语句”来检查两个数组的索引是否匹配,以及它是否创建了一个操作。 谁能帮我弄一下开张的条件吗?现在它给了我“预期表达式”错误 if (_stateList objectAtIndex:[]== _capitalList objectAtIndex[]) { // D
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如果要比较索引
i
中的对象,应使用[[[u stateList objectAtIndex:i]isEqual:[[u capitalList objectAtIndex:i]]
。如果要比较整个数组,可以执行[\u stateList isEqualToArray:\u capitalList]
。这就成功了!非常感谢,现在我可以继续前进了!:)我使用了茨诺里的答案:[[u stateList objectAtIndex:I]isEqual:[[u capitalList objectAtIndex:I]]它工作得很好,谢谢你的输入!