1

I just read the Apple Documentation regarding enums but I'm still somewhat confused.

Say I have the class named Employee. I want the class employee to have an array of tasks (I guess best option is [String]). But I want each task to have a status that's either .Complete or .Incomplete.

I could have another property inside Tasks called taskName, but I think it's easier to just make the Array store Strings

Here's what I thought so far to write:

class Employee {
    class Tasks: Array { // Or [String], I have no idea
        enum Status {
            case Complete
            case Incomplete
        }
    }

    var tasks: Tasks

    init?() {
        self.tasks = Tasks()
    }
}

// Then I guess Employee.tasks[0].Status should work

I know that it's most likely wrong. So how should I do it?

1

1 Answer 1

1

Here is the simple way how you can do.

//Employee Class with Array of Task
class Employee {
    var tasks: [Task]
    init(tasks: [Task]) {
        self.tasks = tasks
    }
}

//Task Class has the status
class Task {
    var status:Status
    var taskName: String
    init(status: Status, taskName: String) {
        self.status = status
        self.taskName = taskName
    }
}

//Status is an enum
enum Status {
    case Complete
    case Incomplete
}

And here is the simple test code...

//Testing 
let task1 = Task(status: Status.Complete, taskName: "First Task")
let task2 = Task(status: Status.Incomplete, taskName: "Second Task")
let employee = Employee(tasks: [task1, task2])

print(employee.tasks[0].status) //Prints -->> Complete
print(employee.tasks[1].taskName)//Prints -->> Second Task
Sign up to request clarification or add additional context in comments.

2 Comments

So if I also want a string with the task, all I need to do is add var taskName: String in Task and then I could access employee.tasks[0].taskName, right?
Yes you can do... I have edited the answer check out... @Lawrence413

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.