GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.