Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Kenan Bateman
Kenan Bateman
16,715 Points

Struct Question

Beginner in Swift here, and I'm struggling with creating rules that reference structs more generally.

Essentially I'm trying to have a base object and then have a second object that can have properties that reference that base object's properties after we determine the instance of that base object.

Specifically trying to create a tool to represent how different matchups happen in Pokemon Go. Each Pokemon has main species stats (base attack, defense, stamina, type). And then each instance of that pokemon has it's own specific moves and independent attack, defense, stamina values. And each pokemon also has independent attack moves.

Right now I have this:

struct Attack {
    let attackName: String
    let attackType: Type
    let attackPower: Int
    let attackEnergy: Int
    let attackTime: Double
}

struct PokemonSpecies {
    let baseAttack : Double
    let baseDefense : Double
    let baseStamina : Double
}

struct Pokemon {
    let species: PokemonSpecies

    var attackIV : Double = 0.0
    var defenseIV : Double = 0.0
    var staminaIV : Double = 0.0
    let quickAttack : Attack
    let chargeAttack : Attack


/// I know this code below doesn't work but hopefully my errors can help show what I'm trying to accomplish 

    func attackValue() -> Double {
        return (self.species.baseAttack + self.attackIV)
    }

    func defenseValue() -> Double {
        return (self.species.baseDefense + self.defenseIV)
    }

    func hpValue() -> Double {
        return (self.species.baseStamina + self.staminaIV)
    }

6 Answers

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

I’m pretty sure the code isn’t working right now only cause in the attack struct. For the stored property attackType you gave it type type. Unless you have another struct Type it’ll make it crash. I’m pretty sure to get properties of one type for a subtype you need to add : then the parent struct name. Pasan talks about this here

https://teamtreehouse.com/library/inheritance-2

Kenan Bateman
Kenan Bateman
16,715 Points

Tricky thing is I need copies of this object, so my understanding was that a class wouldn't work even though that inheritance is what I'm looking for

Kenan Bateman
Kenan Bateman
16,715 Points

Type created earlier in the file, just didn't quote it since it wasn't relevant in the inheritance really I felt

enum Type {
    case normal
    case fighting
    case flying
    case poison
    case ground
    case rock
    case bug
    case ghost
    case steel
    case fire
    case water
    case grass
    case electric
    case psychic
    case ice
    case dragon
    case dark
    case fairy
}
Jonathan Ruiz
Jonathan Ruiz
2,998 Points

Oh okay, hope the video helps

Kenan Bateman
Kenan Bateman
16,715 Points

But structs don't have inheritance?

Jeff McDivitt
Jeff McDivitt
23,970 Points

What errors are you getting?