Segue阵列不通过原因?

时间:2018-04-18 19:10:23

标签: ios swift swift4

我的智慧结束了。我的工作基本相同(但是在测试项目中使用更简单的函数生成变量)但现在我的代码将空白数组传递给下一个视图控制器。

我只想将2个数组(通过函数生成的值)传递给下一个VC - 我已经三次检查了我的代码,我确定我'我做得对,但显然不是!任何想法!?

第一个VC:

import UIKit

class WorkoutSetupController: UIViewController {

@IBOutlet weak var timeInputField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()}

//Create function to generate a new random workout, with a random exercise array, and random reps array

func generateNewWorkout() -> (randomExerciseArray:[String], randomRepsArray:[Int]) {

    let randomKey = Int(arc4random_uniform(4) + 3)
    var workoutSet = [String]()
    let possibleExercises = masterExerciseArray
    var repsSet = [Int]()

    while workoutSet.count < (randomKey) {
        let randomIndex = Int(arc4random_uniform(UInt32(possibleExercises.count)))
        workoutSet.append(possibleExercises[randomIndex])
    }

    while repsSet.count < (randomKey) {
        repsSet.append(Int(arc4random_uniform(30)))
    }

    //return the values and print them to make sure the function is working (it is!)

    let workoutToBePassed = workoutSet
    let repsToBePassed = repsSet
    print (workoutToBePassed, repsToBePassed)
    return (workoutToBePassed, repsToBePassed)

}

//perform a manual segue to the WorkoutController page, using the function above to generate the values to be passed

@IBAction func workoutButtonPressed(_ sender: UIButton) {

    performSegue(withIdentifier: "doItNow", sender: generateNewWorkout())


}

//set the variables to be passed to the new VC, accepting the format of the returned arrays from my function for the variables

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "doItNow" {
        if let destVC = segue.destination as? WorkoutController, let variablesToBePassed = sender as? (arr1:[String],arr2:[Int]) {

            print (variablesToBePassed.arr1, variablesToBePassed.arr2)

            destVC.selectedWorkoutExerciseArray = variablesToBePassed.arr1
            destVC.selectedWorkoutRepsArray = variablesToBePassed.arr2

        }
    }
}
}

第二个VC:

import UIKit

class WorkoutController: UIViewController {

//set the variables we're passing from the first VC with the right data type

var selectedWorkoutExerciseArray = [String]()
var selectedWorkoutRepsArray = [Int]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func printButtonPressed(_ sender: UIButton) {

    //print the variables out - this is currently printing blank arrays arghhghghghh!

    print (selectedWorkoutExerciseArray, selectedWorkoutRepsArray)

}

}

我哪里错了!?我无处可见!请帮忙!

1 个答案:

答案 0 :(得分:2)

元组的参数标签不匹配,您必须检查

let variablesToBePassed = sender as? (randomExerciseArray:[String], randomRepsArray:[Int]) {

这是强行展开对象的好处的一个很好的例子,必须具有预期类型的​​值

  let destVC = segue.destination as! WorkoutController
  let variablesToBePassed = sender as! (arr1:[String],arr2:[Int])

如果代码崩溃,则会立即显示设计错误。