Swift在循环时无法更新struct中的数组

时间:2019-05-03 21:46:44

标签: swift

我有下一个代码:

struct UserWorkoutsStruct {
        var workouts: [WorkoutEntity]
        var jsonKey: String
    }

func makeDataForSections(serverResponse: NSDictionary, completion: @escaping WorkoutsBySections) {

        let mine = UserWorkoutsStruct(workouts: [], jsonKey: "your")
        let last = UserWorkoutsStruct(workouts: [], jsonKey: "last")
        let today = UserWorkoutsStruct(workouts: [], jsonKey: "today")
        let other = UserWorkoutsStruct(workouts: [], jsonKey: "other")

        let allUserWorkouts = [mine, last, today, other]

        for var userWorkout in allUserWorkouts {

            if let workouts = serverResponse[userWorkout.jsonKey] as? [[String: Any]] {
                workouts.forEach { (element) in
                    let workout = WorkoutEntity.mr_import(from: element)
                    userWorkout.workouts.append(workout)
                }
            }
        }

        completion(mine.workouts, today.workouts, last.workouts, other.workouts)
    }

但是所有mine.workouts, today.workouts, last.workouts, other.workouts包含0个元素。

但是在循环中,我看到userWorkout.workouts.append向数组添加了元素。

0 个答案:

没有答案