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

SOLVED: Missing mandatory text data for property '_identifier'

Forums > SwiftUI

Hello, I am strugling with a SwiftData error for about a week now and I am out of ideas. Somehow SwiftData/Xcode has decided to generate an _identifier (Z_IDENTIFIER and Z_NORMALIZEDIDENTIFIER in the sqlite db file) and it's complaining that the value for it is empty. Here is the full error:

error: Row (pk = 1) for entity 'Entry' is missing mandatory text data for property '_identifier'
CoreData: error: Row (pk = 1) for entity 'Entry' is missing mandatory text data for property '_identifier'

Here is my full model file for reference:

import Foundation
import SwiftData

@Model
final class Entry {
    var amount: Double
    var currency: Locale.Currency
    var budget: Budget
    var tags: [Tag] = []
    var date: Date
    var note: String

    init(
        amount: Double,
        currency: Locale.Currency = (Locale.current.currency ?? Locale.Currency.init(stringLiteral: "EUR")),
        budget: Budget,
        tags: [Tag] = [],
        date: Date = Date.now,
        note: String = ""
    ) {
        self.amount = amount
        self.currency = currency
        self.budget = budget
        self.tags = tags
        self.date = date
        self.note = note
    }
}

Any help or directions are extreamly appreciated. Thank you.

   

If anyone stumbles here for a solution, my problem was: var currency: Locale.Currency works with var currency: String.

   

@alec writes:

...my problem was: var currency: Locale.Currency works with var currency: String

I think we can interpret this to mean that SwiftData is happy to store Strings for you.
But it has no idea how to store a struct named Locale.Currency.

Apple's documentation shows that Currency is a struct within Locale in Foundation.

// How? How should SwiftData store this struct within its internal data files?
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
    public struct Currency : Hashable, Codable, Sendable, ExpressibleByStringLiteral {

        /// Creates an instance initialized to the given string value.
        ///
        /// - Parameter value: The value of the new instance.
        public init(stringLiteral value: String)

        public var identifier: String
//.... snip.....

Keep Coding!

Thanks for sharing your findings.

   

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 your entire paywall view without any code changes or app updates.

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.