0
我有一個可以評估運動員的視圖控制器。導航欄中有一個下一個按鈕,用於加載下一位運動員。我試圖通過下面的代碼一步一步通過我的數組,但每次索引返回爲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;
}
怪異的行爲是一個)視圖出現時,無論誰挖的指數是2,和b)不進入下一個運動員的eval,這是同一個人,這是索引中的最後一個人。
我犯過的最愚蠢的錯誤。不知道爲什麼甚至在那裏開始。謝謝。 –