TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Working with Enumerations. 🙄

Forums > SwiftUI

Good evening everyone.

Good afternoon. I'm experimenting with enumerations, and I can't figure out how to get something out of it using a switch or if-else What the hell is happening...

let tasks: [Task] = [
            Task(title: "See lesson: Spinner Using SwiftUI", typeTask: .regularTask(RegularTask())),
            Task(title: "Read about Ad hoc install app", typeTask: .importantTask(ImportantTask(important: .medium))),
            Task(title: "See lesson @Composible Kotlin", typeTask: .regularTask(RegularTask())),
            Task(title: "See WWDC 2024", typeTask: .importantTask(ImportantTask(important: .low))),
            Task(title: "Read about ExperimentalMaterial3Api Kotlin", typeTask: .importantTask(ImportantTask(important: .medium))),
            Task(title: "Finish the test PlantUML", typeTask: .importantTask(ImportantTask(important: .high))),
            Task(title: "Finish the test PlantUML", typeTask: .regularTask(RegularTask()))
        ]

    for index in tasks {
        print(index.typeTask)
        if let task == index.typeTask as? RegularTask {
          print("1")
         }
    }

      // OR

    switch index.typeTask {
      case .regularTask(_):
        print(index.typeTask)
      case .importantTask(_):
        print(index.typeTask)
    }
}
import Foundation

 enum TypeTask {
    case regularTask(RegularTask)
    case importantTask(ImportantTask)
}

struct Task {
    var title: String
    var completed: Status = .notStarted
    var typeTask: TypeTask

    enum Status {
        case notStarted
        case completed
        case canceled
        case paused
    }
}

struct RegularTask { }

struct ImportantTask {
    private let date = Date()
    var important: TaskPriority

    var deadLine: Date {
        switch important {
        case .low:
            return calculateDate(3)
        case .medium:
            return calculateDate(2)
        case .high:
            return calculateDate(1)
        }
    }

    enum TaskPriority: Int {
        case low
        case medium
        case high
    }

    private func calculateDate(_ value: Int) -> Date {
        guard let data = Calendar.current.date(byAdding: .day, value: value, to: Date()) else {
            return Date()
        }
        return data
    }
}

3      

Got it figured out.

indirect enum TypeTask {
    case regularTask(RegularTask)
    case importantTask(ImportantTask)
}

enum Status {
    case notStarted
    case completed
    case canceled
    case paused
}

protocol ProtocolTask {
    var title: String { get set }
    var completed: Status { get set }
    var typeTask: TypeTask { get set }
}

struct Tasks: ProtocolTask {
    var title: String
    var completed: Status
    var typeTask: TypeTask
}

struct RegularTask { }

struct ImportantTask {
    var important: TaskPriority
    var deadLine: String {
        switch important {
        case .low:
            return calculateDate(3)
        case .medium:
            return calculateDate(2)
        case .high:
            return calculateDate(1)
        }
    }

    enum TaskPriority: Int {
        case low
        case medium
        case high

        var imageStatus: String {
            switch self {
            case .low:
                return "❗️"
            case .medium:
                return "❗️❗️"
            case .high:
                return "❗️❗️❗️"
            }
        }
    }

    private func calculateDate(_ value: Int) -> String {
        guard let data = Calendar.current.date(byAdding: .day, value: value, to: Date()) else {
            return ""
        }
        return convertData(data)
    }

    private func convertData(_ date: Date) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd/MM/YYYY, HH:mm a"
        return dateFormatter.string(from: date)
    }
}
    for index in tasks {
        switch index.typeTask {
            case .regularTask:
                print("")
            case .importantTask(var important):
                //print("\(data)"
                print("\(important.deadLine)")
            }
      }

I just don’t understand why Decoder is of no use

    init(from decoder: Decoder) throws {
                let container = try decoder.container(keyedBy: CodingKeys.self)
                let movementType = try container.decode(String.self, forKey: .typeTask)
                switch movementType {
                case "RegularTask":
                    typeTask = .regularTask(RegularTask())
                case "ImportantTask":
                    typeTask = .importantTask(ImportantTask(important: .high))
                default:
                    throw DecodingError.dataCorruptedError(forKey: .typeTask, in: container, debugDescription: "Invalid movement type")
                }

                self.title = ""
                self.completed = .completed
    }

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.