0

Hello I have a swift file that contains arrays of tuples that I reference with my app. For example, my structure looks like:

struct workoutDict {
   static var bicepWorkouts = [("Barbell Curl","Bicep",0,"3x10"), ("x","x",3,"x")]
}

I have a global variable named eqID. For example, eqID = "bicepWorkouts"

There is part of my code where I attempt to call this dictionary. Normally I would use: workoutDict.bicepWorkouts and this works fine, but I have other workouts such as barbellWorkouts in my dictionary so i let the user select "bicepWorkouts", "barbellWorkouts" ect based on user input and need to use that to reference my struct.

Right now i have

//Call the global variable to get what workout to look for
code = eqID
let ourWorkouts = workoutDict.code 

which should set ourWorkouts = workoutDict.bicepWorkouts when our eqID is set to that, but it keeps giving me the error that workoutDict does not contain the code when I use that reference.

I tried researching alternate methods of referencing structs but I couldn't find anything. Any help would be appreciated!

1 Answer 1

1

You should create yet another dictionary to store the names:

struct workoutDict {

    static var dicts = [ "bicepWorkouts": bicepWorkouts, "barbellWorkouts": barbellWorkouts ]

}

Then you can access the sub dictionaries by:

workoutDict.dicts[eqlID]
Sign up to request clarification or add additional context in comments.

3 Comments

Ah that is very simple, thank you! will accep you in 7 minutes
Types, such as enums, classes, structs should have leading capitalised letter, i.e. struct Workout (and naming it WorkoutDict is not so good either, because 1) in Swift we do not abbreviate. and 2) That it contains a Dictionary is an implementation details. )
@Sajjon Thank you for those tips! i will remember that in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.