Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Kenan Bateman
16,715 PointsStruct 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
2,998 PointsI’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

Kenan Bateman
16,715 PointsTricky 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
16,715 PointsType 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
2,998 PointsOh okay, hope the video helps

Kenan Bateman
16,715 PointsBut structs don't have inheritance?

Jeff McDivitt
23,970 PointsWhat errors are you getting?