1

I wanted to know if I can do what the title suggests. Here is some code.

struct Crop {
    var name = String()
    var season = String()
    var reharvistable = Bool()

    //struct Stages:     
    struct Stage1{
        var minDay = Int()
        var maxDay = Int()
        var minWater = Int()
        var maxWater = Int()
    }/* 
    struct Stage2{
        var minDay = Int()
        var maxDay = Int()
        var minWater = Int()
        var maxWater = Int()
    } */
}

var Crops  = [Crop]()
var temp = Crop(name: "Turnip", season: "Spring", reharvistable: false)

Crops.append(Crop(Name: "Turnip", Season: "Spring", Reharvistable: 
     false,  Stage1(minDay: 2, maxDay: 3, minWater: 2, maxWater: 7)))

Stage 2 is commented out but I wanted to have up to Stage 6 in the future.

"temp" works but my attempt to add them to the array didn't. My ultimate goal is to have these "Crop"s to be store in an array, "Crops" so I want to be able to dynamically add more "Crop"s. Also, the parentheses are after each datatype because they were giving me an error without them.

1 Answer 1

3

How about instead of defining the stage structs inside your crop struct, you define them separately and have a stages array inside crop. Like this:

struct Crop {
    var name = String()
    var season = String()
    var reharvistable = Bool()
    var stages = [Stage]()
}

struct Stage {
    var minDay = Int()
    var maxDay = Int()
    var minWater = Int()
    var maxWater = Int()
}

Then you could define a crop like this:

Crops.append(Crop(name: "Turnip", season:" spring", reharvistable: false, stages: [
    Stage(minDay: 2, maxDay: 3, minWater: 2, maxWater: 7),
    ...
])

This way you can have as many stages as you'd like, and add or remove them at will.

Sign up to request clarification or add additional context in comments.

Comments

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.