-1

Hey guys I'm working on my first ever app and need some help. My workout tracker app has two classes which you can find below. I have two view controllers hooked up to their own swift files.

Heres my first view controller

Basically what it does is takes the data in the text fields and steppers and turns it into a "Workout" object then appends it to the "WorkoutList" class array.

I've got a print statement setup that prints the Array.count. It shows the correct number in the debug but when I switch views it gets reset to zero.

  @IBAction func addToWorkoutList(_ sender: UIButton) {

        let workout = Workout(name: workoutName.text!, description: workoutDescription.text!, sets: Int(setStepper.text!)!, reps: Int(repStepper.text!)!)
        workoutArrayList.append(workout)

        print(workoutArrayList.count)
    }

The second view inherits the first views class so that is how I access the "WorkoutArrayList"

class OverViewViewController: NewWorkoutViewController, UITableViewDelegate, UITableViewDataSource {

My app basically allows you to add a workout then produces a graph based on the data you provided. This can make it easy to visualize your gains in the gym. I'm doing this as a learning project so any help on what I should do to build this app would also be greatly appreciated.

Workout Object Class

import Foundation

class Workout {

    let workoutName : String
    let workoutDescription : String
    let numberOfSets : Int
    let numberOfReps : Int

    init(name : String, description : String, sets : Int, reps : Int) {
        workoutName = name
        workoutDescription = description
        numberOfSets = sets
        numberOfReps = reps
    }
}

WorkoutList Class

import Foundation

class WorkoutList {

    let workoutArray : [Workout] = []

}
  • How you are switiching the View??Do you mean pushing the newViewController? – Hussain Shabbir Jul 29 '17 at 01:57
  • Possible duplicate of [How do you share data between view controllers and other objects in Swift?](https://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-controllers-and-other-objects-in-swift) – Shades Jul 29 '17 at 03:10

1 Answers1

0

Inheriting the class is not what you want to do here. An easy fix for you would be to make workoutArray a static variable so any class can access it at any given time.

static var workoutArray : [Workout] = []

Here is why just inheriting the class doesn't work. When the OverViewViewController loads in to the app, it creates a new instance of the class OverViewViewController and since it inherits NewWorkoutViewController, it also creates a new instance of the NewWorkoutViewController class. You have 2 different instances of NewWorkoutViewController, changing a variable in one of those instances won't change it for any other instances of the class. A static variable, however, is what you are looking for. The array can be changed and accessed any time from any class, you don't even need to inherit. It would work whether you made workoutArray static or workoutArrayList static.

If you have any other questions, feel free to leave a comment.

Timmy
  • 537
  • 3
  • 10
  • Thank you for the help. I'm a new developer trying to test the waters of swift. Inheriting the view controller didn't feel right but its all I could think of. Silly me forgot about static variables. – Jafett Puga Jul 29 '17 at 05:56