我有下一个代码:
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
向数组添加了元素。