arrays - 快速显示数组中先前输入的项目

标签 arrays swift uitableview sorting core-data

我正在尝试显示当前锻炼中先前输入的锻炼属性。如果我转到上一次锻炼,所有内容都会显示,但是当我转到当前锻炼时,以前的锻炼属性不会显示,并且日期标签仅显示今天的日期,而不显示以前的锻炼日期。这是该问题的两个函数。如果我需要发布更多内容,请告诉我。

 func lastWorkout() -> Workout? {
    if let client = currentClient(), let currentWorkout = currentWorkout(), let workouts = client.workouts as? Set<Workout> {
        // get all this client's workouts in cronological order
        let sortedWorkouts = workouts.sorted { (one, two) -> Bool in
            let scheduledTimeOfOne = one.appointment?.scheduled ?? Date(timeIntervalSince1970: 0)
            let scheduledTimeOfTwo = two.appointment?.scheduled ?? Date(timeIntervalSince1970: 0)
            return scheduledTimeOfOne > scheduledTimeOfTwo
        }
        // get the index of this workout
        let indexOfTodaysWorkout = sortedWorkouts.index(of: currentWorkout) ?? 0
        // go back one workout to find the last workout completed
        let lastWorkout: Workout? = (indexOfTodaysWorkout - 1) < 0 ? nil : sortedWorkouts[indexOfTodaysWorkout - 1]
        // and return
        return lastWorkout
    }
    return nil
}

/// Last Exercise Info to load previous exercise data
func lastExercise() -> Exercise? {
    guard let selectedExercise = currentExerciseInfo() else{
        return nil
    }
    if let exercises = lastWorkout()?.exercises as? Set<Exercise>, let pastExercise = exercises.first(where: { $0.exerciseInfo == selectedExercise }) {
        return pastExercise
    }
    return nil
}

最佳答案

因此,上次锻炼函数中的数组计数已关闭。这是工作函数的样子。我仍然没有显示正确的日期。它只给出今天的日期。

func lastWorkout() -> Workout? {
    if let client = currentClient(), let currentWorkout = currentWorkout(), let workouts = client.workouts as? Set<Workout> {
        // get all this client's workouts in cronological order
        let sortedWorkouts = workouts.sorted { (one, two) -> Bool in
            let scheduledTimeOfOne = one.appointment?.scheduled ?? Date(timeIntervalSince1970: 0)
            let scheduledTimeOfTwo = two.appointment?.scheduled ?? Date(timeIntervalSince1970: 0)
            return scheduledTimeOfOne > scheduledTimeOfTwo
        }
        // get the index of this workout
        let indexOfTodaysWorkout = sortedWorkouts.index(of: currentWorkout) ?? 0
        // go back one workout to find the last workout completed
        let lastWorkout: Workout? = sortedWorkouts.count < 2 ? nil : sortedWorkouts[indexOfTodaysWorkout + 1]
        // and return
        return lastWorkout
    }
    return nil
}

关于arrays - 快速显示数组中先前输入的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50117631/

相关文章:

ios - 如何创建一个动态更改其高度以适应其内容的表格 View 单元格?

java - "Missing return Statement"但我已经有了 return 语句

python - Numpy 产生错误的值

ios - Firebase:是否可以更新多个父级中的单个子级?

ios - 在 UICollectionView 选择上选择 UITableView

ios - 如何转发委托(delegate)的所有方法?

java - 使用多个 int 数组作为参数

java - 将字节数组写入oracle中的原始列

swift - iPhone X 上的 UITabBarController

swift - Heroku 上的 Vapor 3 : REPL?