UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Grabbing Struct of Struct

Forums > Swift

Hello, let me explain my goal: I have a struct called "Exercise" which represents one full workout like Chest, Legs, Back, etc. I want this exercise to have multiple workouts in it like dumbbell bench press, pushups, etc.

I am having trouble putting my individual workouts into the variable routine of type [workouts]. This is the error I get currently: " Cannot convert value of type 'workouts.DumbbellBenchPress.Type' to expected element type 'Array<workouts>.ArrayLiteralElement' (aka 'workouts') Cannot convert value of type 'workouts.Pushups.Type' to expected element type 'Array<workouts>.ArrayLiteralElement' (aka 'workouts') "

Here's my code:

File 1 : https://pastebin.com/aVGvyWqK File 2: https://pastebin.com/uT60UgV5

Thanks!

2      

I think michaela is having a data modeling problem, not a Swift coding problem.

I am having trouble putting my individual workouts
into the variable routine array of type [workouts].
This is the error I get currently: "
Cannot convert value of type 'workouts.DumbbellBenchPress.Type' to expected element type 'Array<workouts>.ArrayLiteralElement'

Your code:

  // Two data types, but they hold identical data.
struct DumbbellBenchPress {
        let name = "Dumbbell Bench Press"
        let image = "push-ups" 
        let video = ""
        let description = "The Dumbbell Bench Press is similar ... snip ..."
    }

struct Pushups {
        let name = "Pushups"
        let image = "push-ups"
        let video = ""
        let description = "Pushups are one of the best dynamic exerc ... snip...."
    }

You may want to rethink your data models. In this case, both structs have the same content, but have different titles. To Swift you've created TWO different types. These cannot be put into a single array. Because arrays want to store the same types.

Instead, consider creating a struct named Exercise and one of the structs properties is focus. Example:

struct Exercise {
     var focus          = "Pectorals"  // <-- consider making this an enum !
     var name           = "Pushups"
     var image          = "pushups-elbows-in"
     var video: String? = nil // you may not have a video
     var description    = "Also knows as =Let Downs="
}

Then you can put any struct of type Exercise into an array named workout of type [Exercise].

let chestWorkout: [Exercise] = []  // start with empty array. but it can only hold exercises.
let letDowns                 = Exercise(focus: "Pectorals", name: "Let Downs")  // create new exercise
let pushUps                  = Exercise(focus: "Pectorals", name: "Push Ups")   // create another exercise
chestWorkout.append( letDowns )
chectWorkout.append( pushUps )

3      

@michaelacaggi you can put your code directly into your forum post by placing backticks ``` on the line before your code block and the line after. You can manually type the backticks or you can highlight your code and hit the Code button on the toolbar. It looks like this: </>.

This makes it easier for people to help you since they don't have to go to an external site just to see the code you are having an issue with.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.