I am not sure what I changed to start getting a crash when creating one of my objects in SwiftData. When I try to create a new Recipient
I am getting SwiftData/BackingData.swift:386: Fatal error: Unknown related type - Card
The Recipient
is defined as
/// A recipient is someone you have sent a card to. It is the primary view for the application. A recipient must have a name.
@Model
class Recipient {
/// When sending a card, a recipient should have an address. This is the first line, usually the street address.
var addressLine1: String = ""
/// This is the second line, usually the apartment or unit number.
var addressLine2: String = ""
/// This is the city of town for the address.
var city: String = ""
/// This is the state or region for the address.
var state: String = ""
/// This is the Zip or Postal code for the address.
var zip: String = ""
/// This is the Country
var country: String = ""
/// This is the First Name (or names) of the recipient of the card
var firstName: String = ""
/// This is the Last Name of the recipient of the card
var lastName: String = ""
/// A Recipient may have zero or more cards. There is a cascading relationship to the cards
@Relationship(deleteRule: .cascade, inverse: \Card.recipient) var cards: [Card]?
/// This is a computed promperty holidng the full name of the recipient, it is first name followed by a space and then the last name
var fullName: String {
String("\(firstName) \(lastName)")
}
init(addressLine1: String, addressLine2: String, city: String, state: String, zip: String, country: String, firstName: String, lastName: String, cards: [Card]?) {
self.addressLine1 = addressLine1
self.addressLine2 = addressLine2
self.city = city
self.state = state
self.zip = zip
self.country = country
self.firstName = firstName
self.lastName = lastName
self.cards = cards
}
}
and as you can see has an optional relationship with the Card object. Defined as:
/// A card is a specific instance of a greeting card sent to a specific person.
@Model
final class Card {
/// This can either be the date the card is sent or the date of the event the card is for. By default all cards will start with today's date.
var cardDate: Date = Date()
/// A card must be attachd to a specific event type. The event types are definable by the user of the application.
var eventType: EventType? // @Relationship(deleteRule: .nullify, inverse: \EventType.cards)
/// A card will have an image, this is defined as a GreetingCard
var cardFront: GreetingCard?
/// A card must be sent to someone
var recipient: Recipient?
init(cardDate: Date, eventType: EventType, cardFront: GreetingCard, recipient: Recipient) {
self.cardDate = cardDate
self.eventType = eventType
self.cardFront = cardFront
self.recipient = recipient
}
}
Creating a new recipient should work with no values, and my creation that is failing is as follows:
let recipient = Recipient(addressLine1: "", addressLine2: "", city: "", state: "", zip: "", country: "", firstName: "", lastName: "", cards: nil)
modelContext.insert(recipient)
// push to the recipient in the stack
navigationPath.append(recipient)
When the app crashes, Xcode expands the @Relationship
macro as follows:
{
/// A Recipient may have zero or more cards. There is a cascading relationship to the cards
@storageRestrictions(accesses: _$backingData, initializes: _cards)
init(initialValue) {
_$backingData.setValue(forKey: \.cards, to: initialValue)
_cards = _SwiftDataNoType()
}
/// A Recipient may have zero or more cards. There is a cascading relationship to the cards
get {
_$observationRegistrar.access(self, keyPath: \.cards)
return self.getValue(forKey: \.cards)
}
/// A Recipient may have zero or more cards. There is a cascading relationship to the cards
set {
_$observationRegistrar.withMutation(of: self, keyPath: \.cards) {
self.setValue(forKey: \.cards, to: newValue) // ERROR SHOWS UP HERE
}
}
}
And the error happens in the setter. Given that a new recipient doesn't have any cards, why can't the array be nil?