CoreData: fault: Could not materialize Objective-C class named “Array” from declared attribute value type “Array<String>”
Error
While creating a new iOS App using AI, I encountered above console error on Xcode.
Recently I am working on iCloud backup support using SwiftData.
So I thought it would be related to the task.
But the reason was that words property is String Array.
@Model
final class WordSetModel {
var name: String
@Relationship var words: [String]Problem
We need to add @Relationship attribute to use array for the property in a SwiftData model.
Let’s see words again:
@Relationship var words: [String]This means WordSet have a relationship with Word, one to many
However String is not Table, so this can’t be work.
Solutions
How to solve?
1. create WordModel
Instead of using String, create a child model
@Model
final class WordModel {
var text: String
init(text: String, wordSet: WordSetModel? = nil) {
self.text = text
self.wordSet = wordSet
}
}2. json property
Store array as JSON string.
@Model
final class WordSetModel {
var name: String
var words: String
var wordList: [String] {
// return parse words..
}AI’s solution
WordModel
@Model
final class WordModel {
var text: String
@Relationship(inverse: \WordSetModel.words) var wordSet: WordSetModel? // WordSetModel과 관계 설정
...
}WordSetModel
@Model
final class WordSetModel {
var name: String
@Relationship(inverse: \WordModel.wordSet) var words: [WordModel]
...
}AI’s solution leads another error
Circular reference resolving attached macro ‘Relationship’
So I asked AI how to solve this and he answered.
WordModel
@Relationship(inverse: \WordSetModel.words, deleteRule: .nullify) var wordSet: WordSetModel? // WordSetModel과 관계 설정WordSetModel
@Relationship(inverse: \WordModel.wordSet, deleteRule: .cascade) var words: [WordModel]?However both Parent and child can’t have Relationship inverse parameter All.
I had to fix manually.
I Removed inverse from children, WordModel.
@Relationship var wordSet: WordSetModel? // WordSetModel과 관계 설정If you found this post helpful, please give it a round of applause 👏. Explore more Tuist-related content in my other posts.
