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 Swift Enums and Structs Structs and their Methods Review and Final Exam

Final Exam: Can we do this without initializing the struct?

The difference between my approach and Amit's is that I didn't initialize the struct, which meant that I had to pass each task two parameters instead of just the description. Is this ok to do?

enum Status {
    case Doing, Pending, Completed

    init() {
        self = .Pending // By default, task will always have pending status
    }
}

struct Task {
    let Task1: (description: String, Status)
    let Task2: (description: String, Status)
    let Task3: (description: String, Status)
}

var Task1 = ("Do laundry", Status.Doing)
var Task2 = ("Make dinner", Status.Pending)
var Task3 = ("Clean room", Status.Completed)

1 Answer

Hi David,

I'm not sure your code solves the problem in hand. It is very close, but not quite there.

The struct is never used, rather than remaining uninitialised. The struct is defining a set of three tuples, each holding a string description and a Status. To initialise the struct requires you to provide values for all three Task items for each instance. So, adding a new Todo Task means you have to define three.

The struct should contain a description and a status. Each instance of the struct then has those properties. Each instance is then its own entity; a combination of the two properties.

As it stands, the Task1 inside the struct is unrelated to the Task1 declared as a variable. Try it; type Task1 and you'll get the tuple of "Do laundry" and (Enum value) representing the Status enum as a whole. Next, delete Task1 from inside the struct. Again, type Task1 to evaluate it - you'll get the same output.

Amit's suggested solution allows you to create as many tasks as you like; all with the same properties and capabilities. Your struct offers that, to an extent, but requires each instance to have three tuples for each instance. The names Task1, Task2, and Task3 do not relate to the name of the struct, though.

Amit's solution is nice an neat and encompasses the requirements set out at the beginning of the challenge. Your solution is so very close but not quite there! The enum is fine; the struct is a little off and the declaration of each task is, unfortunately, unrelated to the other aspects of the code.

I hope that helps.

Steve.