UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftData

Forums > Swift

Good morning all,

I am writing an app using SwiftData. I have a enum type:

enum WhenToPay: Identifiable, CaseIterable, CustomStringConvertible, Codable {
    var id: Self { self }
    case midMonth
    case endOfMonth

    var description: String {
        switch self {
        case .midMonth:
            return "Mid Month"
        case .endOfMonth:
            return "End Of Month"
        }
    }
}

My SwiftData model is as follows:

@Model
class BillSD {
    var name: String = ""
    var amount: Double = 0.0
    var paid: Bool = false
    var month: BillMonth = .january
    var whenToPay: WhenToPay = .endOfMonth
    @Relationship(.nullify, inverse: \SectionSD.bills) var section: SectionSD? = nil

    init(name: String, amount: Double, paid: Bool, month: BillMonth, whenToPay: WhenToPay) {
        self.name = name
        self.amount = amount
        self.paid = paid
        self.month = month
        self.whenToPay = whenToPay
    }
}

I get the folowing error that I have no idea why. As long as the Enum conforms to Codable than I should be able to use it within my SwiftData model. But the model gives me this error, can anyone provide help with this: Type 'Any' has no member 'endOfMonth' then under that error it says In expansion of macro 'Model' here

Thanks Taz

3      

SwiftData is not working with enum as of Xcode 15 beta 5. You can try the following work around using rawValue.

enum WhenToPay: String, Identifiable, CaseIterable, CustomStringConvertible, Codable {
    var id: Self { self }
    case midMonth
    case endOfMonth

    var description: String {
        switch self {
        case .midMonth:
            return "Mid Month"
        case .endOfMonth:
            return "End Of Month"
        }
    }
}
@Model
class BillSD {
    var name: String = ""
    var amount: Double = 0.0
    var paid: Bool = false
    var month: BillMonth.RawValue
    var whenToPay: WhenToPay.RawValue
    @Relationship(.nullify, inverse: \SectionSD.bills) var section: SectionSD? = nil

    init(name: String, amount: Double, paid: Bool, month: BillMonth, whenToPay: WhenToPay) {
        self.name = name
        self.amount = amount
        self.paid = paid
        self.month = month.rawValue
        self.whenToPay = whenToPay.rawValue
    }
    @Transient
    var whenToPayType: WhenToPay {
        WhenToPay(rawValue: whenToPay) ?? .endOfMonth
    }
    @Transient
    var billMonthType: BillMonth {
        BillMonth(rawValue: month) ?? .january
    }
}

3      

Thank you for the reply, so im assuming that in code when i want to refer to WhenToPay I will need to use those new @Transient properties?

Thanks, Taz

3      

I couldnt come up with a better name for the @Transient properties, so sorry about that. You can use both actually. The difference is that if you use the original property, you will get the String rawValues. But if you use the @Transient properties you will get the enum type. For example

let billSD = BillSD()
billSD.month // it will give you for example the string rawValue january
billSD.billMonthType // it will give you for example .january

I hope this makes sense.

3      

enums are working properly when working with swiftData. we don't need that solution anymore with Xcode 15 beta 8.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free 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.