【发布时间】:2017-03-30 04:58:36
【问题描述】:
我想创建一个包含 3 个随机值的新数组,该数组从此列表中提取,用于其他两个视图控制器。不确定如何分配数组以便可以访问这些值。
class WorkoutDataSource {
var allWorkouts:[Workout]
init() {
allWorkouts = []
let bh1 = Workout(title: "Figure 8s - Clockwise", workoutText: "Clockwise around each leg.", color: UIColor.flatNavyBlueColorDark())
allWorkouts.append(bh1)
let bh2 = Workout(title: "Figure 8s - Counter Clockwise", workoutText: "Counter Clockwise around each leg.", color: UIColor.flatNavyBlue())
allWorkouts.append(bh2)
let bh3 = Workout(title: "Dribble Left Handed", workoutText: "Low and Powerfull.", color: UIColor.flatTealColorDark())
allWorkouts.append(bh3)
let bh4 = Workout(title: "Dribble Right Handed", workoutText: "Low and powerfull", color: UIColor.flatTeal())
allWorkouts.append(bh4)
let bh5 = Workout(title: "Around Both Feet", workoutText: "With feet together, circles around ankles-waist-head, work up & down, the full length of body.", color: UIColor.flatSkyBlueColorDark())
allWorkouts.append(bh5)
let bh6 = Workout(title: "Spider Dribble", workoutText: "feet apart, 1 dribble with each hand in front, then 1 dribble with each hand in back. ", color: UIColor.flatSkyBlue())
allWorkouts.append(bh6)
let bh7 = Workout(title: "Helicopter", workoutText: "1 hand in front, 1 hand in back – move your hands from front to back while catching the ball between your legs.", color: UIColor.flatGreenColorDark())
allWorkouts.append(bh7)
let bh8 = Workout(title: "Scissors - Legs Not Moving", workoutText: "One leg forward and one leg back. Dribble between your legs low and fast.", color: UIColor.flatGreen())
allWorkouts.append(bh8)
let bh9 = Workout(title: "Behind the Back", workoutText: ".",color: UIColor.flatGray())
allWorkouts.append(bh9)
let bh10 = Workout(title: "Fingertip Squeeze", workoutText: ".",color: UIColor.flatPink())
allWorkouts.append(bh10)
}
func getWorkOuts() -> [Workout]{
return allWorkouts
}
}
谢谢
【问题讨论】:
-
当您说“此列表”时,您所说的列表是什么?您是指您显示的方法中的文本吗?
-
对不起,我不清楚。我想随机抽出 3 个锻炼。
-
我添加了一个答案,其中涵盖了我能想到的基本场景 - 希望这会有所帮助。但如果没有,请告诉我:)
-
你应该使用数组字面量,而不是像那样重复附加值。
标签: arrays swift dictionary random