简体   繁体   English

创建随机数量的类实例-Swift

[英]Create random number of class instances - Swift

I'm trying to generate a random 'workout' with a random number of exercises. 我正在尝试通过随机练习生成一个随机的“锻炼”。

Each exercise needs to have a name (from an array) and a number of reps (a random number which will eventually have 'upper and lower' bounds depending on what type of exercise it is (eg up to 10 for push ups, but only 1 for '500m row'. But that can come later! For now, I'm trying to work out how to generate a random number of class instances (I'll then pass these via a segue and put in a tableView, assuming that's possible) 每个练习都需要有一个名称(来自数组)和多个代表(一个随机数,最终取决于练习的类型而具有“上限和下限”(例如,俯卧撑最多10个,但仅限于此) 1代表“ 500m行”,但是稍后可能会出现!现在,我正在尝试找出如何生成随机数量的类实例的方法(然后我将通过segue传递这些实例并将其放入tableView中,假设那是可能)

Here's my code, which is just in a playground for now : 这是我的代码,目前仅在操场上:

import UIKit

let exerciseArray = ["squats", "pushups", "lunges", "jumping jacks"]

class exerciseInWorkout {

    var exerciseName : String
    var exerciseReps : Int

    init(name: String, reps: Int) {
        exerciseName = name
        exerciseReps = reps
    }

}

let randomkey = Int(arc4random_uniform(4))
let numberOfExercisesInWorkout = Int(arc4random_uniform(10))

// Manually creating a new object of exerciseInWorkout with a random exercise and a random number of reps

let exerciseOne = exerciseInWorkout(name:exerciseArray[randomkey],     reps:Int(arc4random_uniform((30))))

//print result to make sure it works

print(exerciseOne.exerciseName, exerciseOne.exerciseReps)

//Have some function here which creates a number of class instances based on the "numberOfExercisesInWorkout" constant

If I'm completely on the wrong track here feel free to tell me but I think I'm not far off (hopefully...) 如果我完全走错了路,请随时告诉我,但我认为我离我们并不遥远(希望...)

There are probably a lot of different (valid) approaches to this. 可能有很多不同的(有效)方法。 One of which could be something like this: 其中之一可能是这样的:

let exerciseArray = ["squats", "pushups", "lunges", "jumping jacks"]

struct Exercise {
    let name: String
    let reps: Int
}

func randomInt(upperBound: UInt32) -> Int {
    return Int(arc4random_uniform(upperBound))
}

let numberOfExercisesInWorkout = randomInt(upperBound: 10)

let exercises: [Exercise] = (1...numberOfExercisesInWorkout).map { _ in
    let randomKey = randomInt(upperBound: UInt32(exerciseArray.count))
    return Exercise(name: exerciseArray[randomKey], reps: randomInt(upperBound: 10))
}

print(exercises)

Which ouputs something like this: 输出如下内容:

[Exercise(name: "lunges", reps: 8), Exercise(name: "jumping jacks", reps: 6), Exercise(name: "lunges", reps: 4), Exercise(name: "squats", reps: 9), Exercise(name: "jumping jacks", reps: 5), Exercise(name: "squats", reps: 9)] [运动(名称:“ lunges”,次数:8),运动(名称:“ jumping jacks”,次数:6),运动(名称:“ lunges”,次数:4),运动(名称:“ squats”,次数:9),运动(名称:“ jumping jacks”,次数:5),运动(名称:“ squats”,次数:9)]


Note : You could also modify the ( randomInt ) helper function to take into account a lowerBound I guess 注意 :您也可以修改( randomInt )辅助函数以考虑我想的lowerBound。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM