NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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!

   

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 )

1      

@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.

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.