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

Enum, Struct + Switch -> change enum value in a Struct using switch as method?

Hi guys, i am really excited about this swift course, i am at the enums and structs part, and i wanted to test my self and taking as a starting point the "TO-DO" task given i tried to go a little further. What i wanted to accomplish is to create a method that will switch to the next Status (enum) So, it should be something like this newTask.status // Pending newTask.nextStatus() // Pending -> Process newTask.status // Process

So far, it works, but my method is not replacing the old status with the new one, it just says the new status while i run it thru the method, but i am not being able to store that new value in my variable.

Here´s my code

enum Status{
    case Pending, Process, Completed

    init(){
    self = .Pending
    }

}


struct Task{
    var description: String
    var status = Status()

    init(description: String){
    self.description = description

    }
    func taskStatus() -> String{
        return "Your task: \(self.description) is \(self.status)"

    }


    func nextStatus() -> Status{

        switch Status() {
        case .Pending:
            return .Process
        case .Process:
            return .Completed
        default:
            return .Pending
        }
    }


}
var newTask = Task(description: "Buy food")
newTask.status // Pending
newTask.nextStatus() // Process
newTask.status // Peding

I would really appreciate hints or help here Sorry for my english, I hope i made my self clear!

Thanks in advance!

1 Answer

Well, i´ve been playing around and also kept moving with the following course about Classes, and found out the solution to my problem. I turned my struct into a class, and my method instead of returning a value, i just set it to modify the stored data

Here is the code in case someone finds it useful!

enum Status{
    case Pending, Process, Completed

    init(){
    self = .Pending
    }

}


class Task{
    var description: String
    var status = Status()

    init(description: String){
    self.description = description

    }
    func taskStatus() -> String{
        return "Your task: \(self.description) is \(self.status)"

    }



    func nextStatus(){
        print("Switching status...")
        switch Status() {
        case .Pending:
            status = .Process
        case .Process:
            status = .Completed
        default:
            status = .Pending
        }
    }


}
var newTask = Task(description: "Buy Food")

newTask.status // Pending
newTask.nextStatus() // Pending -> Process
newTask.status // Process

I am not sure if it is the best way to do it, and would be very happy is someone want to send a tip, or correct my approach!

Thanks