gpt4 book ai didi

ios - 无法找到对象在数组中的位置?

转载 作者:行者123 更新时间:2023-11-29 03:39:01 26 4
gpt4 key购买 nike

我有一个 View Controller ,可以在其中评估运动员。导航栏中有一个下一步按钮,应该加载下一个运动员。我正在尝试使用以下代码单步执行我的数组,但每次索引都返回 2?我不知道为什么!我错过了什么吗?

-(void)viewDidAppear:(BOOL)animated{
NSUInteger editIndex = [_athleteArray indexOfObject:_currentAthlete];
NSUInteger index = editIndex-1;

Athlete *count = _athleteArray[index];

NSLog(@"Current Athlete:%@ Index: %lu",count,(unsigned long)index);
}


-(void)whenNextButtonIsTapped{
//stuff
NSUInteger editIndex = [_athleteArray indexOfObject:_currentAthlete];
NSUInteger index = editIndex-1;

Athlete *count = _athleteArray[index];

NSLog(@"Current Athlete:%@ Index: %lu",count,(unsigned long)index);

if(index <= ((_athleteArray.count)-1)){
index++;
_currentAthlete = _athleteArray[index];
_evalSelected = [self getMostRecentEval];
[self updateInformation];
NSLog(@"Index after being smaller than the array count: %lu",(unsigned long)index);
}
else{
if(index == ((_athleteArray.count)-1)){
_evalSelected = [self getMostRecentEval];
[self updateInformation];
index=0;
NSLog(@"Index after being equal to array count: %lu",(unsigned long)index);
}
}
self.title = [NSString stringWithFormat:@"%@'s Evaluation",_evalSelected.whosEval.full];


}

}


-(Eval *)getMostRecentEval{
NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whosEval == %@", _currentAthlete];
[request setPredicate:predicate];
NSEntityDescription *eval = [NSEntityDescription entityForName:@"Eval" inManagedObjectContext:_managedObjectContext];
[request setEntity:eval];



NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"date_recorded"
ascending:NO
selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

NSError *error = nil;

NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil){
//handle error
}

NSMutableArray *lastEvalArray = mutableFetchResults;

return lastEvalArray.firstObject;

}

奇怪的行为是a)当 View 出现时,无论谁被点击,索引都是2。b)它不会进入下一个运动员的评估,它是同一个人,并且是最后一个人索引。

最佳答案

您正在将 editIndex 定义为当前运动员的索引。然后从中减去 1 以获得索引,然后加 1(使用索引++),最后将 currentAthlete 设置为 _athleteArray[index],这将是您开始时使用的同一运动员。您应该能够通过根本不使用索引来修复它,只需使用 editIndex。

关于ios - 无法找到对象在数组中的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18730778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com